object | Object for which the attribute is to be checked . |
name | name of the the attribute . |
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.
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.