我个人对陷阱的定义是这样的:代码看起来可以工作,但不是以你“想当然“”的方式。如果一段代码直接出错,抛出了异常,我不认为这是陷阱。比如,Python程序员应该都遇到过的“UnboundLocalError”, 示例:>>> a=1>>> def func():... a+=1... print a... >>> func()traceback (most recent call last):File "", line 1, in File "", line 2, in funcUnboundLocalError: local variable 'a' referenced before assignment 对于“UnboundLocalError”,还有更高级的版本:import randomdef func(ok): if ok: a = random.random() else: import random a = random.randint(1, 10) return afunc(True)# UnboundLocalError: local variable 'random' referenced before assignment可能对于很多python新手来说,这个Error让人摸不着头脑。但我认为这不算陷阱
………………………………