Python idioms, 2 of n
来源:互联网 发布:淘宝店铺怎样设置积分 编辑:程序博客网 时间:2024/05/22 00:56
1) Don't do trivial assignment in __init__ of the class
Don't:
class Foo(object):
def __init__(self):
self.attr1 = 0
self.attr2 = ''
self.attr3 = []
self.attr4 = {}
Don't:
class Foo(object):
def __init__(self):
self.attr1 = 0
self.attr2 = ''
self.attr3 = []
self.attr4 = {}
Do:
class Foo(object): // Define the member variables when first used in the code
def __init__(self):
...
2)
Try coding like this:
for work_request in iter(queue.get, sentinel):
process_work_request(work_request)
Compare:
while True:
work_request = queue.get()
if work_request == sentinel:
break
process_work_request(work_request)
3) Always put parentheses around "conditoinal expression"
x = (true_value if condition else false_value)
Refer to
http://www.python.org/dev/peps/pep-0308/
Reference
- Python idioms, 2 of n
- Python idioms, 1 of n
- 10 Python pitfalls - Python idioms, 4 of n
- Code Like a Pythonista: Idiomatic Python - Python idioms, 3 of n
- A collection of design patterns/idioms in Python
- English Idioms of Emotion
- Python Idioms and Efficiency
- 2 c program idioms
- Idioms
- Python Performance 2 of n - Python Performance Tips, Part 1
- Python Performance Tips, Part 2 - Python Performance 3 of n
- Python performance 1 of n
- Quick Python Performance Optimization - Python Performance 4 of n
- I/O performance in Python - Python Performance 5 of n
- ZOJ 3175 Number of Containers(n/1+n/2+...+n/(n-1)+n/n求法)
- Python: how to create a list of n lists [[],[],[]]
- Python Tips, Tricks, and Idioms and Learn Python Through Public Data Hacking
- zoj 3175 Number of Containers (nbut1375) 计算n /1+n/2+n/3+n/4....+n/n
- 基于ok6410的韦东山驱动视频简要分析--ts驱动
- android系统下文件擦除
- MySQL索引的创建、删除和查看
- 第七章 函数(part7) 指向函数的指针
- JAVA NIO 简介
- Python idioms, 2 of n
- VC中的*.aps,*.ncb.*.opt文件分别是做什么用的
- xcode4.下使用Three20屏幕无法响应旋转
- svn插件汉化
- 自定義ListActivity(一)
- cocos2d的单例
- 变量命名规则
- 触摸屏驱动分析(s3c-ts.c)(Linux)(分析)
- 大端模式与小端模式、网络字节顺序与主机字节顺序(经典)