1)__
-- 对返回值不感兴趣的变量,例子
def fun()
return 1.2.3
fi1,__,__ =fun()
2)每个class和实例都有一个__dict__
>>> class A:
... def __init__(self):
... self.a=10
... self.b=100
... def fun1(self):
... return 1
...
>>> A.__dict__
mappingproxy({'fun1': <function A.fun1 at 0x0000000000C46510>, '__module__': '__
main__', '__doc__': None, '__dict__': <attribute '__dict__' of 'A' objects>, '__
init__': <function A.__init__ at 0x0000000000C46620>, '__weakref__': <attribute
'__weakref__' of 'A' objects>})
>>> for k,v in A.__dict__.items():
... print(k,v)
...
fun1 <function A.fun1 at 0x0000000000C46510>
__module__ __main__
__doc__ None
__dict__ <attribute '__dict__' of 'A' objects>
__init__ <function A.__init__ at 0x0000000000C46620>
__weakref__ <attribute '__weakref__' of 'A' objects>
>>> a = A()
>>> a.__dict__
{'a': 10, 'b': 100}
>>> a.__dict__.get('a')
10
>>> a
<__main__.A object at 0x0000000000C505C0>
>>> a.a
10
>>> a.b
100
3)可以随时添加属性
>>> a.c=1100
>>> a.__dict__
{'a': 10, 'c': 1100, 'b': 100}
>>> A
<class '__main__.A'>
4)类变量,每个实例拷贝一份。如果直接指定A.p1进行操作,则指向同一个地方,见教程
>>> class A:
... p1 = 100
... def __init__(self):
... self.a = 1000
...
>>> a.p1 = 2000
>>> A.p1
100
>>> a = A()
>>> a.p1
100
>>> a.p1 = 1000
>>> a.p1
1000
>>> A.p1
100
>>> aa = A()
>>> aa.p1 = 2000
>>> A.p1
100
>>> aa.p1
2000
>>> a.p1
1000
>>> A.p1 += 1
>>> A.p1
101
>>> a.p1
1000
>>> aa.p1
2000
5)_默认为上次输出的值
>>> _
2000
>>> _
2000
>>> a.p1
1000
>>> _
1000
>>> def fun():
... return 1,2,3
...
>>> f1,_,_ = fun()
>>> f1
1
>>> _
3
for i,info in enumerate(self.colorList): info.number = i+1