Saturday, 5 December 2015

Python: Check whether an object is instance of class or not


‘isinstance(object, classinfo)’ method is used to check whether an object is instance of given class or not. Return true if the object argument is an instance of the classinfo argument, or of a subclass thereof, else false.
data={1:"hari", 2:"Krishna"}

class Employee:
    def __init__(self, id, firstName, lastName):
     self.id = id
     self.firstName = firstName
     self.lastName = lastName

emp=Employee(1, "Hari", "Krishna")

print(isinstance(emp, Employee))
print(isinstance(emp, dict))
print(isinstance(data, dict))

Run above program, you will get following output.
True
False
True



Previous                                                 Next                                                 Home

No comments:

Post a Comment