Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python hasattr() Function

Python hasattr() Function

Python built-in function Python built-in function


Description

hasattr() The function determines whether an object has the specified attribute.

Syntax

hasattr syntax:


hasattr(object, name)

Parameters

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

(

)

print

(

hasattr

(

point1

,

'

x

'

)

)

print

(

hasattr

(

point1

,

'

y

'

)

)

print

(

hasattr

(

point1

,

'

z

'

)

)

print

(

hasattr

(

point1

,

'

no

'

)

)

No such attribute

Output result:


True
True
True
False

Python built-in function Python built-in function