Python - isinstance()更深刻的理解
作者:互联网
起因经过
今天翻collections.abc的文档时,我知道list的实例在逻辑上(因为duck typing 鸭子类型)是Container和不能hash的(因为list可变),就试下面的代码是否能行:
from collections.abc import Container, Hashable
print(isinstance(list(), Container)) # return true
print(isinstance(list(), Hashable)) # return False
我就产生疑惑,问题1来了:list是继承于object, 而collections.abc.Container是个抽象类,isinstance为什么能正确运行?换另外一句话说,此时我的理解是根据isintance的语义来的,list并没有继承这个抽象类,why isintance() works?
class list(object):
...
class Container(metaclass=ABCMeta):
...
解决
查找isinstance()文档:
Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof.
问题2来了,什么是virutal subclass? 刚好virutal那里有个超链接,点进去
ABCs introduce virtual subclasses, which are classes that don’t inherit from a class but are still recognized by isinstance() and issubclass();
结合以上引用的两点,加深了我对isintance的认识:
isinstance支持direct(直接继承), indirect(非直接继承), virtual(抽象类)。以前对于isinstance的理解只局限在前两种。
标签:Container,Python,深刻,list,isintance,collections,抽象类,isinstance 来源: https://www.cnblogs.com/allen2333/p/11394262.html