i2tutorials

Hasattr() method in Python Built in Function

Hasattr() method in Python Built in Function

 

hasattr method in built in function returns TRUE if the specified object has the specified attribute, if not it will return FALSE.

 

Syntax:

hasattr(object, name)

 

Parameters

hasattr has two parameters.

 

Example-1:

 

Giving the specified object in hasattr method

class Person:
name = "Jay"
age = 36
country = "Norway"
x = hasattr(Person, 'age')
print(x)

 

Output:

Hasattr () method in Python Built in Function

 

Example-2:

 

Not giving the specified object in hasattr method.

age = 24
globals()['age'] = 30
print('The age is:', age)

 

Output:

Exit mobile version