python-nonlocal

Python: Unexpected behaviour of nonlocal variable in recursion

Python: Unexpected behaviour of nonlocal variable in recursion Question: The following code is expected to set the nonlocal variable flag to true after 3 calls to recur(). I expect flag in the following code to be always true after recur(2) returns (starting from 0) def f(): flag = False def recur(n): nonlocal flag print(f"this is …

Total answers: 1

Python inner function sets vs nonlocal

Python inner function sets vs nonlocal Question: I believe I know the answer to this, but wanted to double-check because I find this a bit confusing. def outerFunc(): mySet = set() a = 0 def innerFunc(): mySet.add(1) mySet.add(2) a = 7 innerFunc() print(mySet) # {1, 2} print(a) # 0 Here, if I want to change …

Total answers: 1

When to use nonlocal keyword?

When to use nonlocal keyword? Question: 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 count and total variables here (variable referenced before assignment): def calculate_mean(): count = 0 total = 0 def mean(value): count …

Total answers: 1

SyntaxError: name 'v' is used prior to nonlocal declaration

SyntaxError: name 'v' is used prior to nonlocal declaration Question: I looked at similar problems, but I could not find solutions for my mistake in them. Problem: In the StartPage block, I enter the data for the graph, which is based on these values ​​in the PageOne block, but the data of the first block …

Total answers: 3

How nonlocal keyword works?

How nonlocal keyword works? Question: 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 nonlocal in average(). But in the below code def makeAverage(): count = 0 total = 0 def average(newValue): nonlocal count, total count += …

Total answers: 1

Python: modify method-local variable inside inner method

Python: modify method-local variable inside inner method Question: I’m writing a test method called, say, test_foo (using pytest). I am testing the behavior of a function, foo, which takes as an argument another function, get. foo calls get iteratively, conditionally on the return value of get, e.g.: def foo(get, param): max_num_tries = 3 curr_num_tries = …

Total answers: 1

When does my function will go to previous scopes to look for a variable?

When does my function will go to previous scopes to look for a variable? Question: in python 3.0 from what i know when i’m using variables that aren’t found in the local scope it will go all the way back until it reaches the global scope to look for that variable. I have this function: …

Total answers: 1

keep a variable not-local but not global in python 3

keep a variable not-local but not global in python 3 Question: So I have a small bit of code in python 3.4.1 where I’m just playing with closures def bam(x): def paw(): x+=1 print(x) def bang(): x+=1 print(x) return paw, bang originally I wanted to see if I could call a=bam(56) a[0]() a[0]() a[0]() a[0]() …

Total answers: 1

SyntaxError nonlocal python in a method defenition

SyntaxError nonlocal python in a method defenition Question: 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 nearest enclosing scope for this method …

Total answers: 2

why this python program have the following output?

why this python program have the following output? Question: def makeInc (x, step): def next(): nonlocal x, step x = x + step return x return next x = makeInc (0, 1) y = makeInc (0, 10) x1=x() x2=x() y1=y() y2=y() print( x1, x2, y1, y2) The output is 1 2 10 20. I am …

Total answers: 1