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

Python: modify method-local variable inside inner method

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,…
LateCoder
  • 2,163
  • 4
  • 25
  • 44
0
votes
1 answer

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

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: def make_withdraw(balance): def…
argamanza
  • 1,122
  • 3
  • 14
  • 36
0
votes
1 answer

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

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…
Jim Jones
  • 2,568
  • 6
  • 26
  • 43
0
votes
1 answer

why this python program have the following output?

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 not…
loop
  • 219
  • 2
  • 5
  • 12
-1
votes
1 answer

How to use local, non-local and global variables in the same inner function without errors in Python?

When trying to use the local and non-local variables x in inner() as shown below: x = 0 def outer(): x = 5 def inner(): x = 10 # Local variable x += 1 print(x) nonlocal x # Non-local variable …
-1
votes
3 answers

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

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 cannot be…
Mxxn
  • 13
  • 6
-3
votes
1 answer

How/what is the value of a variable in a nested function declared nonlocal set to?

def make_test_dice(*outcomes): """Return a die that cycles deterministically through OUTCOMES. >>> dice = make_test_dice(1, 2, 3) >>> dice() 1 >>> dice() 2 >>> dice() 3 >>> dice() 1 """ assert…
JamesMat
  • 7
  • 2
1 2 3 4
5