例如:>>> def outer():
... x = 0
... y = (x for i in range(10))
... del x
...
SyntaxError: can not delete variable 'x' referenced in nested scope
基本上这意味着你不能删除内部块中使用的变量(在这种情况下是genexp)。在
注意,这适用于python<;=2.7.x和python<;3.2。
在python3.2中,它不会引发语法错误:
^{pr2}$
请参见this链接以了解整个更改过程。在
我认为python3.2的语义更为正确,因为如果在函数外部编写相同的代码,它就可以工作:#python2.7
>>> x = 0
>>> y = (x for i in range(10))
>>> del x
>>> y.next() #this is what I'd expect: NameError at Runtime
Traceback (most recent call last):
File "", line 1, in
File "", line 1, in
NameError: global name 'x' is not defined
将相同的代码放入函数中时,不仅会更改异常,而且错误发生在编译时。在