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.



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.




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.


 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’)) 


 Traceback (most recent call last): File "c:", 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 

The hasattr () method in Python hasattr: Questions

The hasattr () method in Python Python functions: Questions


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Best laptop for Zoom


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method