Change language

The hasattr () method in Python

|

In the world of object-oriented programming, we are concerned with representing or mapping real-world scenarios to classes and objects. Objects can be thought of as a class outline that displays attributes and their behavior.

Sometimes we may run into situations where we need to check for the existence of an attribute that is occupied or contained in a class. The same can be done using Python’s hasattr () method. This helps in checking if the attribute is in the class.

 Python Hasattr () method



The Python class represents attributes and their behavior through an object.

The hasattr () meth method od is used to check if an attribute exists in a class.

 hasattr (Class, attribute) 

hasattr ( ) returns a boolean, i.e. True or False, depending on the presence of the attribute in the class.

Example 1:

 class Info: name = "JournalDev "lang =" Python "site =" Google "print (hasattr (Info,’ lang’)) 

In the above example, the lang attribute is contained in the Info class. So the hasattr () function returns True.

Output:

 True 

Example 2 :

 class Info: name = "JournalDev" lang = "Python" site = "Google" print (hasattr (Info, ’date’)) 

As you can see from the above example, the function returns False because the date attribute is not defined in the class.



Output:

 False 

Comparison

In Python 2 hasattr () suppresses all exceptions and returns False for the condition.

For example, if the given attribute is & # 8216; A & # 8217; contained in the class, but is concerned about some exceptions. At this point, hasattr () will ignore all exceptions and return False even if the "A" attribute exists in the class.

On the other hand, in Python 3, hasattr () throws an exception if the attribute is associated with some exceptional criteria .

Example: Python 2 with hasattr () function

 class Info (object): @property def hey (self ): raise SyntaxError def say (self): raise SyntaxError obj = Info () print (hasattr (obj, ’hey’)) print (hasattr (obj,’ say’)) 

In the above code despite a syntax error due to the decorator, the hasattr () method throws no errors, ignores the exception, and returns False even if the class contains that particular attribute.

Exit:

 False True 

Example: Python 3 with hasattr ()

In the code below, the hasattr () function raises an exception by reason for a syntax error with the attribute & # 8216; hey & # 8217 ;.

 class Info (object): @property def hey (self): raise SyntaxError def say (self): raise SyntaxError obj = Info () print (hasattr (obj, ’hey’)) print (hasattr (obj,’ say’)) 

Output:

 Traceback (most recent call last): File "c: usershpappdatalocalprogramspythonpython36libsite-packagesIPythoncoreinteractiveshell.py", line 3319, in run_code exec (code_obj, self.user_global_ns, self.usert_ns) File "& lusert_ns -20-e14f6e57b66e" ", line 9, in "module" print (hasattr (obj, ’hey’)) File" "ipython-input-20-e14f6e57b66e" ", line 4, in hey raise SyntaxError File" "string" ", line unknown SyntaxError