Questions tagged [python-nonlocal]

The python 3 nonlocal keyword marks a variable as one taken from a parent scope, and is similar to the global keyword.

The nonlocal keyword (introduced in Python 3), lets you mark a variable as one that is to be taken from a parent scope. It can only be used in a nested function and the variable must exist in the nesting scope (unlike the global keyword).

67 questions
464
votes
10 answers

What does "nonlocal" do in Python 3?

What does nonlocal do in Python 3.x? To close debugging questions where OP needs nonlocal and doesn't realize it, please use Is it possible to modify variable in python that is in outer, but not global, scope? instead. Although Python 2 is…
ooboo
  • 16,259
  • 13
  • 37
  • 32
194
votes
9 answers

Is it possible to modify a variable in python that is in an outer (enclosing), but not global, scope?

Consider this example: def A(): b = 1 def B(): # I can access 'b' from here. print(b) # But can i modify 'b' here? B() A() For the code in the B function, the variable b is in a non-global, enclosing (outer)…
grigoryvp
  • 40,413
  • 64
  • 174
  • 277
127
votes
10 answers

nonlocal keyword in Python 2.x

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?
adinsa
  • 1,271
  • 2
  • 9
  • 3
26
votes
5 answers

Why doesn't Python's nonlocal keyword like the global scope?

In Python 3.3.1, this works: i = 76 def A(): global i i += 10 print(i) # 76 A() print(i) # 86 This also works: def enclosing_function(): i = 76 def A(): nonlocal i i += 10 print(i) # 76 A() print(i) #…
qntm
  • 4,147
  • 4
  • 27
  • 41
17
votes
4 answers

What is the difference between non local variable and global variable?

I'm learning the concepts of programming languages. I found the terminology "nonlocal" in python syntax. What is the meaning of nonlocal in python?
15
votes
2 answers

Python nonlocal statement in a class definition

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…
Andyrooger
  • 6,748
  • 1
  • 43
  • 44
14
votes
2 answers

syntax error on nonlocal statement in Python

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…
JasperTack
  • 4,337
  • 5
  • 27
  • 39
13
votes
2 answers

Python, how can I change value of a variable in the parent scope?

for example: assginment statement will declare a new local variable. foo = 'global' def func1(): foo = 'func1' def func2(): foo = 'local variable in func2' use global declaration will use the foo in global: def func2(): global…
netroyal
  • 257
  • 3
  • 10
7
votes
1 answer

Where is nonlocals()?

How do I obtain the non-local variables for the current scope? The functions vars, locals, and globals exist, but is there a function to get the nonlocals? Why aren't the nonlocals listed when calling vars? Update My issue is that there's no way to…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
5
votes
3 answers

nested function change variable in an outside function not working

def some_func(a): def access_a(): print(a) access_a() outputs the value of a. However, if I want to change a in the nested function like this: def some_func(a): def change_a(): a += 1 print(a) …
herbert
  • 65
  • 2
  • 6
4
votes
1 answer

How nonlocal keyword works?

In the below code, def makeAverage(): series = [] def average(newValue): series.append(newValue) total = sum(series) return total/len(series) return average python interpreter does not expect series to be…
overexchange
  • 15,768
  • 30
  • 152
  • 347
4
votes
1 answer

How do I access outer functions variables inside a closure(python 2.6)?

From wikipedia I need to access outer functions variables in a similar manner as using the 'nonlocal' keyword from python 3.x. Is there some way to do that in python 2.6? (Not necessarily using the nonlocal keyword)
Thiago Padilha
  • 4,590
  • 5
  • 44
  • 69
4
votes
2 answers

SyntaxError nonlocal python in a method defenition

I'm typing in an interactive mode the following code: class A: a=42 def foo(): nonlocal a but I've a SyntaxError: no binding for nonlocal 'a' found. But I'm expected that the result of resolution nonlocal a will be 42, because the…
user2953119
3
votes
3 answers

Is there something like 'nonlocal' in Python < 3?

I got a piece of code like this: foo = None def outer(): global foo foo = 0 def make_id(): global foo foo += 1 return foo id1 = make_id() # id = 1 id2 = make_id() # id = 2 id3 = make_id() # ... I…
wal-o-mat
  • 7,158
  • 7
  • 32
  • 41
3
votes
1 answer

using nonlocal or global inside a class body

In python it is valid to use an import statement inside a class to define class variables that are just stolen from other modules: class CustomMath: from math import pi assert isinstance(CustomMath.pi, float) # passes It is also valid to refer…
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
1
2 3 4 5