python tips:类的专有属性

实例通常能够调用类的属性,但是有些属性是类专有的,实例无法调用。

实例调用方法时查找属性时,首先在自己的__dict__中找,找不到去类中找,在类中能够找到的属性都位于dir(cls)中,如果类的某些属性不在dir(cls)中,那么实例无法访问。

 1 >>> class A:
 2     pass
 3 
 4 >>> A.__name__ # the name of class, function, method, descriptor, generator
 5 'A'
 6 >>> A.__qualname__ # qualified name (path from global scope to here)
 7 'A'
 8 >>> A.__mro__       # look for base classed during method resolution
 9 (<class '__main__.A'>, <class 'object'>)
10 >>> A.mro()            # result is stored in __mro__
11 [<class '__main__.A'>, <class 'object'>]
12 >>> A.__subclasses__()  # immediate subclasses
13 []

 

posted @ 2019-06-10 20:29  luoheng  阅读(187)  评论(0编辑  收藏  举报