issubclass()

Check if the input object is subclass of input classinfo.

class : class to be checked for subclass against classinfo
classinfo : class to be checked against this classinfo. Can be a tuple of more than one class

issubclass returns True if class is subclass of given classinfo, False otherwise.
issubclass(class ,classinfo)
We created a parent ( base ) class animals(), then we derived sub class or child class wild(). Then we created birds() as child class of wild()

We will use issubclass() to check our classes against different classinfo.
class animals():
    home='Jungle' #class attribute
#instance attributes
    def __init__(self,name,height):
        self.name= name
        self.height=height
class wild(animals):
    weight=2
class birds(wild):
    name='Eagle'

print(issubclass(wild,animals))  # True
print(issubclass(birds,wild))    # True
print(issubclass(birds,animals)) # True
print(issubclass(animals,wild))  # False
print(issubclass(wild,wild))     # True
print(issubclass(wild,(str,list,animals))) # True

Using tuple

Check the last line of above code , we used one tuple to check the class against more than one classinfo . Here it is again
print(issubclass(wild,(str,list,animals))) # True

Using isinstance()

By using isinstance() we can check if the object is instance of the class or not.

All Built in Functions in Python locals() dir() classmethod


plus2net.com



Post your comments , suggestion , error , requirements etc here




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