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


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer