类中的特殊成员
一些类中的特殊成员:
|
|
[('__class__', <class 'type'>), ('__delattr__', <slot wrapper '__delattr__' of 'object' objects>), ('__dict__', mappingproxy({'__dict__': <attribute '__dict__' of 'Foo' objects>, '__doc__': ' 这是一个注释 ', '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'Foo' objects>, 'f': <function Foo.f at 0x048408A0>, 'name': ''})), ('__dir__', <method '__dir__' of 'object' objects>), ('__doc__', ' 这是一个注释 '), ('__eq__', <slot wrapper '__eq__' of 'object' objects>), ('__format__', <method '__format__' of 'object' objects>), ('__ge__', <slot wrapper '__ge__' of 'object' objects>), ('__getattribute__', <slot wrapper '__getattribute__' of 'object' objects>), ('__gt__', <slot wrapper '__gt__' of 'object' objects>), ('__hash__', <slot wrapper '__hash__' of 'object' objects>), ('__init__', <slot wrapper '__init__' of 'object' objects>), ('__le__', <slot wrapper '__le__' of 'object' objects>), ('__lt__', <slot wrapper '__lt__' of 'object' objects>), ('__module__', '__main__'), ('__ne__', <slot wrapper '__ne__' of 'object' objects>), ('__new__', <built-in method __new__ of type object at 0x69C5BAD0>), ('__reduce__', <method '__reduce__' of 'object' objects>), ('__reduce_ex__', <method '__reduce_ex__' of 'object' objects>), ('__repr__', <slot wrapper '__repr__' of 'object' objects>), ('__setattr__', <slot wrapper '__setattr__' of 'object' objects>), ('__sizeof__', <method '__sizeof__' of 'object' objects>), ('__str__', <slot wrapper '__str__' of 'object' objects>), ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x04B725B8>), ('__weakref__', <attribute '__weakref__' of 'Foo' objects>), ('f', <function Foo.f at 0x048408A0>), ('name', '')] |
|
['__dict__', '__doc__', '__module__', '__weakref__', 'name']
__dict__ | In [27]: Foo.__dict__ Out[27]: mappingproxy({'__dict__': <attribute '__dict__' of 'Foo' objects>, '__doc__': ' 这是一个注释 ', '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'Foo' objects>, 'f': <function __main__.Foo.f>, 'name': ''}) 是用来存储对象属性的一个字典,其键为属性名,值为属性的值 |
__doc__ | In [31]: Foo.__doc__ Out[31]: ' 这是一个注释 ' 表示类的描述信息 |
__module__ | In [32]: Foo.__module__ Out[32]: '__main__' 表示从哪个模块导入的 |
__weakref__ | 看这个就好了: 其实就是没啥用 https://stackoverflow.com/questions/36787603/what-exactly-is-weakref-in-python |
'name' | 其实就是我们定义的静态字段 |
def __init__(self): super().__init__()
|
|
|
用于迭代器,之所以列表、字典、元组可以进行for循环,是因为类型内部定义了 __iter__
In [46]: [1,2,3].__iter__() Out[46]: <list_iterator at 0x4b7e6b0>
In [47]: iter([1,2,3]) Out[47]: <list_iterator at 0x4b7e790> |