if key in map:
p = map[key]
else:
p=0.0
上面这段代码可以简化为:
p = map.get(key,0.0)
python class 中的静态方法装饰器
@staticmethod
def dispatcherTread():
while(1):
....
异常处理
try:
f = open(filename,'r')
except IOError as e:
print e
手工引发异常
raise RuntimeError("Computer says no")
多线程互斥锁使用(虽然python不建议使用多线程,因为GIL的原因)
import threading
message_lock = theading.Lock()
...
with message_lock:
messages.add(newmessage)
with 语句或自动获取 message_lock 对象,离开with 代码块后,还会自动释放这个对象。
with语句通常用于系统资源或者执行环境相关的对象,如文件,连接,锁定。也能自定义。
获得帮助,打印__doc__属性即可
print aa.__doc__