python笔记

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


http://www.runoob.com/python/python-tutorial.html
Python教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值