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

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

isinstance and issubclass

   1  
   2  >>> class P(object):  # parent class
   3  	pass
   4  
   5  >>> class K(P):       # subclass
   6  	pass
   7  
   8  >>> k = K()           # instace
   9  >>> isinstance(k, K)
  10  True
  11  >>> isinstance(k, P)
  12  True
  13  >>> isinstance(K, P)  # K is a class
  14  False
  15  
  16  >>> issubclass(K, P)
  17  True
  18  >>> issubclass(k, P)  # k is not a class
  19  
  20  TypeError: issubclass() arg 1 must be a class
  21  
  22  >>> isinstance(K, type)  # a class is an instace of type
  23  True
  24  >>> isinstance(k, type)  # not a class
  25  False
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS