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
0
votes
1 answer

How to update nonlocal variables while caching results?

When using the functools caching functions like lru_cache, the inner function doesn't update the values of the non local variables. The same method works without the decorator. Are the non-local variables not updated when using the caching…
Mihir
  • 71
  • 1
  • 1
  • 7
0
votes
1 answer

SyntaxError: no binding for nonlocal 'i' found

I want to define a static varaible i (I want only one copy of i through all the recursive calls to that function). For that I've declared 'i' just below the class but outside the function. To use the declared 'i', I have used the keyword 'nonlocal…
techtie
  • 11
  • 4
0
votes
1 answer

When to use nonlocal keyword?

I don't understand why I can use series variable here: def calculate_mean(): series = [] def mean(new_value): series.append(new_value) total = sum(series) return total/len(series) return mean But I can't use…
user3565923
  • 153
  • 2
  • 12
0
votes
1 answer

How to call a parent variable in a nested function

I've written a denoising function with cv2 and concurrent.futures, to be applied on both my training and test image data. The functions (at current) are as follows: def denoise_single_image(img_path): nonlocal data img =…
Yehuda
  • 1,787
  • 2
  • 15
  • 49
0
votes
1 answer

binding of nonlocal for specific function

x = 0 def outer(): x = 1 def inner(): nonlocal x x = 2 def vnat(): nonlocal x x = 5 print('vnat:', x) vnat() print('inner:', x) inner() …
0
votes
1 answer

Using nonlocal inside exec

Why does the following code: exec(""" a = 3 def b(): nonlocal a a = a + 1 b() #error occurs even without this call print(a) """ ) ) give this error: Traceback (most recent call last): File "", line 1, in File "",…
kmindspark
  • 487
  • 1
  • 7
  • 18
0
votes
2 answers

Invalid syntax using += operator

I keep getting a syntax error when using += in python here is my code. I am also having troule with nonlocal. I am getting lots of errors including syntax errors and unbound local error Thanks in advance!!! update: more code added this is all the…
joeisme1
  • 35
  • 5
0
votes
1 answer

Accessing the outer function element inside the nested one

I have a problem with my code. Because I can't access the list from outer function inside the nested one. I tried nonlocal but it gives me an error "non binding for nonlocal "a" found. def func(x, y): nonlocal a nonlocal b …
GKroch
  • 73
  • 1
  • 9
0
votes
1 answer

Why does untriggered assignment changed `nonlocal` behavior?

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 =…
Zen
  • 4,381
  • 5
  • 29
  • 56
0
votes
2 answers

How to use nonlocal statement with several inner functions?

Might someone explain me why this function is not working ? Shouldn't the "nonlocal" statement makes x understandable in g, and therefore in h ? def f(): def g(): nonlocal x x= 1 def h(): print(x) >>> SyntaxError:…
Doe Jowns
  • 23
  • 3
0
votes
3 answers

How to change outer function variabe?

In "Learning Python" by Mark Lutz I read: "Functions can freely use names assigned in syntactically enclosing functions and the global scope, but they must declare such nonlocals and globals in order to change them" I failed to test them in Python…
0
votes
1 answer

Python: modify nonlocal variable after return from outer function

Consider the following example: def g(): in_g=100 def f1(): nonlocal in_g in_g = 10 def f2(): nonlocal in_g print(in_g) return (f1,f2) (f1, f2) = g() f2() #prints 100 f1() f2() #prints 10 Both…
user42768
  • 1,951
  • 11
  • 22
0
votes
2 answers

UnboundLocalError when using += on list. Why is `nonlocal` needed here when directly calling __iadd__ works fine?

Consider this code: def main(): l = [] def func(): l += [1] func() print(l) if __name__ == '__main__': main() It will produce: Traceback (most recent call last): File…
tahsmith
  • 1,643
  • 1
  • 17
  • 23
0
votes
1 answer

Scope error working with imported module in Python 3

I have an imported function from a separate file I have previously created. In it I declare the variable nums to be nonlocal and then work with it. So with that context I will give you a snippet of my code and my error. cwd =…
Gdfelt
  • 161
  • 15
0
votes
4 answers

How to access nonlocal scope inside class definition in python?

I want to do this (dummy example): def func(): nonlocal var print (var) class A: var = 'hola' func() But I get: "SyntaxError: no binding for nonlocal 'var' found" What I really intend to do is append a method name to a list in the…
nadapez
  • 2,603
  • 2
  • 20
  • 26