Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

isinstance and issubclass (See related posts)

>>> class P(object):  # parent class
	pass

>>> class K(P):       # subclass
	pass

>>> k = K()           # instace
>>> isinstance(k, K)
True
>>> isinstance(k, P)
True
>>> isinstance(K, P)  # K is a class
False

>>> issubclass(K, P)
True
>>> issubclass(k, P)  # k is not a class

TypeError: issubclass() arg 1 must be a class

>>> isinstance(K, type)  # a class is an instace of type
True
>>> isinstance(k, type)  # not a class
False

You need to create an account or log in to post comments to this site.


Click here to browse all 5059 code snippets

Related Posts