isinstance(object,classinfo)

  1. 返回True,如果object是classinfo或者classinfo子class的实例。
  2. 如果classinfo是包含type和class的tuple,只要满足其中一个的实例,就返回True。
  3. object不是实例,返回False。
  4. classinfo不是class或者type,触发TypeError。
  5. >>> isinstance(2,int)
    True
    >>> isinstance(2,(int,float))
    True
    >>> isinstance(2,[int,float])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

     

issubclass(class,classinfo)

>>> class a:
...     pass

>>> class b(a):
...     pass

>>> class c(a):
...     pass
... 
>>> issubclass(a,a)
True

>>> issubclass(a,b)
False

>>> issubclass(b,a)
True

>>> issubclass(b,[a,c])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: issubclass() arg 2 must be a class or tuple of classes
>>> issubclass(b,(a,c))
True

>>> issubclass(b,(c,a))
True

 

posted on 2015-12-27 22:17  保质期两天  阅读(343)  评论(0编辑  收藏  举报