python-nonlocal

Why does untriggered assignment changed `nonlocal` behavior? [duplicate]

假如想象 提交于 2019-12-02 22:59:25
问题 This question already has answers here : In Python, variables inside if conditions hide the global scope even if they are not executed? (4 answers) Closed last year . Today I'm reading python change log and meet the nonlocal keyword and did some experiment with it. I find a confusing situation where untriggered assignment will change the nonlocal keyword behavior, please see the example below. def a(): x = 'a' def b(): def c(): nonlocal x x = 'c' c() b() print(x) a() >>> python3 test.py c def

Why does untriggered assignment changed `nonlocal` behavior? [duplicate]

邮差的信 提交于 2019-12-02 08:55:42
This question already has an answer here: In Python, variables inside if conditions hide the global scope even if they are not executed? 4 answers Today I'm reading python change log and meet the nonlocal keyword and did some experiment with it. I find a confusing situation where untriggered assignment will change the nonlocal keyword behavior, please see the example below. def a(): x = 'a' def b(): def c(): nonlocal x x = 'c' c() b() print(x) a() >>> python3 test.py c def a(): x = 'a' def b(): def c(): nonlocal x x = 'c' c() if False: x = 'b' b() print(x) a() >>> python3 test2.py a You can

syntax error on nonlocal statement in Python

时间秒杀一切 提交于 2019-11-30 23:01:12
问题 I would like to test the example of the use of the nonlocal statement specified in the answer on this question: def outer(): x = 1 def inner(): nonlocal x x = 2 print("inner:", x) inner() print("outer:", x) but when I try to load this code, I always get a syntax error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "t.py", line 4 nonlocal x ^ SyntaxError: invalid syntax Does anybody know what I am doing wrong here (I get the syntax error for every example that I

Python nonlocal statement in a class definition

别说谁变了你拦得住时间么 提交于 2019-11-30 19:16:51
I'm trying to perform some analysis of scope in Python 3 source code and I'm stuck with how the nonlocal statement statement works inside a class definition. As I understand it, the class definition executes its body inside a new namespace (call it dict) and binds the class name to the result of type(name, bases, dict). Nonlocal x should work as long as it refers to a variable that is bound somewhere in the enclosing non-local scope. From this I expect the following code to compile and run: class A: v = 1 class B: nonlocal v v = 2 but this fails with SyntaxError: no binding for nonlocal 'v'

Python nonlocal statement in a class definition

孤者浪人 提交于 2019-11-30 16:51:45
问题 I'm trying to perform some analysis of scope in Python 3 source code and I'm stuck with how the nonlocal statement statement works inside a class definition. As I understand it, the class definition executes its body inside a new namespace (call it dict) and binds the class name to the result of type(name, bases, dict). Nonlocal x should work as long as it refers to a variable that is bound somewhere in the enclosing non-local scope. From this I expect the following code to compile and run:

nonlocal keyword in Python 2.x

本秂侑毒 提交于 2019-11-26 11:39:48
I'm trying to implement a closure in Python 2.6 and I need to access a nonlocal variable but it seems like this keyword is not available in python 2.x. How should one access nonlocal variables in closures in these versions of python? Chris B. Inner functions can read nonlocal variables in 2.x, just not rebind them. This is annoying, but you can work around it. Just create a dictionary, and store your data as elements therein. Inner functions are not prohibited from mutating the objects that nonlocal variables refer to. To use the example from Wikipedia: def outer(): d = {'y' : 0} def inner():

nonlocal keyword in Python 2.x

瘦欲@ 提交于 2019-11-26 02:29:35
问题 I\'m trying to implement a closure in Python 2.6 and I need to access a nonlocal variable but it seems like this keyword is not available in python 2.x. How should one access nonlocal variables in closures in these versions of python? 回答1: Inner functions can read nonlocal variables in 2.x, just not rebind them. This is annoying, but you can work around it. Just create a dictionary, and store your data as elements therein. Inner functions are not prohibited from mutating the objects that

Python nonlocal statement

本秂侑毒 提交于 2019-11-26 00:08:47
问题 What does the Python nonlocal statement do (in Python 3.0 and later)? There\'s no documentation on the official Python website and help(\"nonlocal\") does not work, either. 回答1: Compare this, without using nonlocal : x = 0 def outer(): x = 1 def inner(): x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x) # inner: 2 # outer: 1 # global: 0 To this, using nonlocal , where inner() 's x is now also outer() 's x : x = 0 def outer(): x = 1 def inner(): nonlocal x x = 2