/  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.

  • Object:this is the parameter whose named attribute is to be checked in the program.
  • Name: this is the parameter whose attributed need to be searched.

 

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:

Hasattr () method in Python Built in Function