Hey, readers! In this article, we will unveiling the behavior of Python hasattr() method in detail.
Need of Python hasattr() method
In the world of Object-oriented programming, we deal with the representation or mapping of real-life scenarios to Classes and Objects. The objects can be considered as a blueprint of a class that depicts the attributes and its behavior.
At times, we may come across situations wherein we need to check for the presence of an attribute occupied or contained by a Class. The same can be achieved through Python hasattr() method. It helps in checking for the presence of an attribute in a Class.
Now that we have understood the necessity and origin of Python hasattr() method, let us understand the working of the same.
Working of Python hasattr() method
Python Class represents the attributes and its behavior through an object.
The hasattr() method
is used to check for the presence of an attribute within a Class.
hasattr(Class, attribute)
The hasattr() method returns a boolean value i.e. either 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 attribute ‘lang’ is contained by the class ‘Info’. Thus, the hasattr() function returns True.
Output:
True
Example 2:
class Info:
name = "JournalDev"
lang = "Python"
site = "Google"
print(hasattr(Info, 'date'))
As seen in the above example, the hasattr() function returns False because the attribute ‘date’ is not defined in the class.
Output:
False
Python 2 hasattr() method v/s Python 3 hasattr() method
In Python 2, the hasattr() overpowers all the exceptions and returns False for a condition.
For example, if a given attribute ‘A’ is contained by a class but is preoccupied by some exceptions. At this moment, the hasattr() will neglect all the exceptions and will return False even if the attribute ‘A’ happens to exist in the Class.
On the other hand, in Python 3, the hasattr() raises an exception if the attribute is involved with some exceptional criterias.
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 of the syntax error due to the decorator, the hasattr() method does not raise any error and neglects the exception and returns False even if the class happens to contain that particular attribute.
Output:
False
True
Example: Python 3 with hasattr() function
In the below code, the hasattr() function raises an exception error for the Syntax Error cause with the attribute ‘hey’.
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:\users\hp\appdata\local\programs\python\python36\lib\site-packages\IPython\core\interactiveshell.py", line 3319, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-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
Conclusion
Thus, in this article, we have understood the working of Python hasattr() with Python version 2 and 3.
References
- Python hasattr() method — JournalDev