You should in general eschew type-checking in favour of duck typing.
However, if you really need to branch on multiple types, the lack of function overloading means that you need to use type checking.
Method 1: Use
This returns the actual class of the object which you can compare to a class. It will compare equal only if it is the exact class.
class Parrot(object): pass class NorwegianBlue(Parrot): pass def main(): p1 = Parrot() p2 = NorwegianBlue() print type(p1) == Parrot print type(p2) == Parrot if __name__ == '__main__': main()
Method 2: Use
This will return
True if the object is an instance of the class or classes provided, or one of their subclasses:
print isinstance(p1, Parrot) print isinstance(p2, Parrot) print isinstance(p2, NorwegianBlue)
True True True
Reference:2. Built-in Functions