四十三、isinstance方法
1、定义:
isinstance(...) isinstance(object, class-or-type-or-tuple) -> bool Return whether an object is an instance of a class or of a subclass thereof. With a type as second argument, return whether that is the object's type. The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for isinstance(x, A) or isinstance(x, B) or ... (etc.).
2、解释:返回bool类型。用来判断一个对象是否是一个类的实例或者子类的实例,或者判断是否为某种对象类型。
3、举例:
>>> isinstance('aa',basestring) True >>> isinstance(u'aa',basestring) True >>> isinstance(1,(basestring,int)) True >>> isinstance('aa',str) True >>> isinstance(u'aa',str) False
四十四、issubclass方法
1、定义:
issubclass(...) issubclass(C, B) -> bool Return whether class C is a subclass (i.e., a derived class) of class B. When using a tuple as the second argument issubclass(X, (A, B, ...)), is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).
2、解释:从名字可以看出,此方法与上面的isinstance作用相似:即用来判断一个类是否是一个类的子类或者继承类。
3、举例:
>>> issubclass(str, unicode) False >>> issubclass(str, basestring) True >>> issubclass(str, (unicode,basestring)) True
Copyright © 2021.aoyanming个人博客站
发表评论