Python hasattr() Function
Description
hasattr() The function determines whether an object has the specified attribute.
Syntax
hasattr syntax:
hasattr(object, name)
Parameters
- object -- the object.
- name -- a string, the attribute name.
Return Value
Returns True if the object has the attribute, otherwise returns False.
Example
The following examples demonstrate how to use hasattr:
#!/usr/bin/python
-- coding: UTF-8 --
class
Coordinate
:
x
=
10
y
= -
5
z
=
0
point1
=
Coordinate
(
)
(
hasattr
(
point1
,
'
x
'
)
)
(
hasattr
(
point1
,
'
y
'
)
)
(
hasattr
(
point1
,
'
z
'
)
)
(
hasattr
(
point1
,
'
no
'
)
)
No such attribute
Output result:
True
True
True
False
Python built-in function