Python的程序结构[2] -> 类/Class[0] -> 类的特殊属性

类的特殊属性 / Special Property of Class


Python 中通过 class 进行类的定义,类可以实例化成实例并利用实例对方法进行调用。

类中还包含的一些共有的特殊属性。

特殊类属性

含义

__name__

类的名字(字符串)

__doc__ 

类的文档字符串

__bases__

类的所有父类组成的元组

__dict__

类的属性组成的字典

__module__

类所属的模块

__class__

类对象的类型

 1 class Foo():
 2     """
 3     This is the text that can be called by __doc__
 4     """
 5     def __init__(self):
 6         self.foo = None
 7 
 8     def foo_method(self):
 9         self.foom = True
10 
11 print('>>>', Foo.__name__)
12 print('>>>', Foo.__doc__)
13 print('>>>', Foo.__bases__)
14 print('>>>', Foo.__dict__)
15 print('>>>', Foo.__module__)
16 print('>>>', Foo.__class__)

上面的代码定义了一个 Foo 类,并对类的基本特殊属性依次进行调用,最后得到结果如下,

>>> Foo
>>> 
    This is the text that can be called by __doc__
    
>>> (<class 'object'>,)
>>> {'__dict__': <attribute '__dict__' of 'Foo' objects>, '__doc__': '
    This is the text that can be called by __doc__
    ', '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__init__': <function Foo.__init__ at 0x0301F930>, '__module__': '__main__', 'foo_method': <function Foo.foo_method at 0x0305C348>}
>>> __main__
>>> <class 'type'>
原文地址:https://www.cnblogs.com/stacklike/p/8098067.html