hasattr()
hasattr(object, name) Returns Boolean ( True or False ) after checking attribute of the input object
object | Object for which the attribute is to be checked . |
name | name of the the attribute . |
Read more on Class Object and Attributes →
Output of hasattr() is True or False.
class Animal():
#class attributes
species='carnivores'
#instance attributes
def __init__(self1,name1,age):
self1.name= name1
self1.age=age
#instantiate the Animal class
tiger=Animal("Ronald",5)
print(hasattr(tiger,'age')) # True
print(hasattr(tiger,'height')) # False
In above code we got True for the attribute age , but got False for attribute height as it is not declared.
«All Built in Functions in Python setattr() delattr() getattr()
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com