python if判断风格
使用startswith() endswith()代替切片符
- if foo.startswith('bar'): 而不是 if foo[:3] == 'bar':
使用isinstance() 代替直接比较
- if isinstance(obj, int):而不是 if type(obj) == type(1):
特别在比较字符串的时候,可能是unicode类型,他们两的共同祖先就是basestring
if isinstance(obj, basestring)
对于字符串,列表,元组判断是否为空不用len
- if not seq: if seq:而不是 if len(seq) if not len(seq)
不要拿bool变量直接和True和False比较
- if greeting: 而不是 if greeting == True: 更不是 if greeting is True:
对于字符串的遍历不要依赖空白符,一些编辑器可能会不识别或者直接忽略
转载于:https://blog.51cto.com/3502990/653263