python 中需要养成的便捷方法

1,tuple 的多赋值 (a,b,c) = c,d,e

2,ls = [i for i in range(10)]  str = ["%s = %s" %(key ,value ) for key, value in ]

3,getattr() 函数,ex:getattr([],'pop')

4,可以使用 ["%s"%format]来做字符串的替换。

5,lambda 函数没有 函数名字

6,and-or 技巧 可以 充当c中的 bool?a:b ex:

1 a = "a"
2 b = "b"
3 1 and a or b # print a
4 2 and a or b # print b

此方法不安全,当a为None的时候,安全的方法是(1 and [a] 0r [b])[0](1) 在 lambda中不能使用if的时候可以作为选择使用。

例如 在

def processFunc(collapse):
     return collapse and (lambda s:" ".join(s.split())) or (lambda s:s)

 7,__init__ 方法是可选的,但一旦你定义了就必须记得显示调用父类方法。

8,python 中 str的判断相等,is,== 是内容相等和java 不一致

9,专用方法,前后各有两个下划线。

原文地址:https://www.cnblogs.com/harveyaot/p/2983673.html