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
1
vote
1 answer

Python scope, functions, blocks and non-local

I am confused about scope. What, if anything, is the difference between assigning a variable 'within a function' and assigning one within an indented block? I have read many places that if and try blocks do not create, or have, their own scope, but…
Malik A. Rumi
  • 1,855
  • 4
  • 25
  • 36
1
vote
2 answers

dot notation in Python's function definition

I know Python supports object-oriented structure, which uses dot notation. However, I feel confused about the code below where dot notation appears in a function definition with no class defined. Is that some feature defined as function attributes…
H.Li
  • 33
  • 6
1
vote
2 answers

Why does Python 3.8.0 allow to change mutable types from enclosing function scope without using "nonlocal" variable?

In the following Python 3.8.0.0 script, its is not allowed to change immutable variables from enclosing function scope from sub/nested function, however, modifying mutable types elements works perfectly fine without using nonlocal declaration from…
1
vote
3 answers

Can I get the value of a non-local variable without using the nonlocal statement?

I have a local variable x = "local" which unfortunately shares its name with both a global and a non-local variable. Without changing any of the names, can I access all three values? For x = "global" there is globals(), but what about the non-local…
finefoot
  • 9,914
  • 7
  • 59
  • 102
1
vote
0 answers

No binding for nonlocal variable in Python callback function

I have the following code for receiving messages from RabbitMQ message queue: import pika HOST = 'localhost' QUEUE = 'hello' with pika.BlockingConnection(pika.ConnectionParameters(host=HOST)) as conn: with conn.channel() as channel: …
Rudolf Lovrenčić
  • 147
  • 1
  • 2
  • 9
1
vote
1 answer

Python 3 changing variable in function from another function

I would like to access the testing variable in main from testadder, such that it will add 1 to testing after testadder has been called in main. For some reason I can add 1 to a list this way, but not variables. The nonlocal declaration doesn't work…
Uninvolved
  • 15
  • 1
  • 5
1
vote
1 answer

Can I get access to variable different from nonlocal or global in python3.4?

Can I access to other variable in specific scope? Like this: variable = 'acces with global' def function1: variable = 'function 1 variable' def function2: variable = 'I really like this name for variable' def function3: …
Hiperon43
  • 215
  • 1
  • 6
0
votes
1 answer

Why doesn't the `nonlocal` keyword propogate the outer-scoped variable to the calling module?

So this involves a maybe unusual chain of things: A.py from B import call def make_call(): print("I'm making a call!") call(phone_number="867-5309") B.py def call(phone_number): pass test_A.py import pytest from A import…
AmagicalFishy
  • 1,249
  • 1
  • 12
  • 36
0
votes
2 answers

Python nonlocal variable value retained after multiple decorator calls

Sorry for the disturbing title, but I can't put the right words on what I've been looking at for hours now. I'm using a decorator with optional parameters and i want to alter one of them within the wrapped function so that each call ends up doing a…
SOKS
  • 190
  • 3
  • 11
0
votes
0 answers

Why global variable is giving an error: name 'po' is not defined. Did you mean:

I am a python newbie, so question could be so simple sorry for that already. class Solution: po = 0 def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: dic = {v : i for i, v in enumerate(inorder)} …
0
votes
0 answers

Local vs non-local vars in a nested function

Why the following code doesn't work as it is but would work after commenting either print(x) or x=1? def f(): x = 1 def g(): print(x) x = 1 g() f()
MOHOSHA
  • 99
  • 8
0
votes
1 answer

Python: Unexpected behaviour of nonlocal variable in recursion

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

What does exactly "nonlocal" keyword do with a variable?

Here is an example code that I have made to try to understand the mechanics of "nonlocal" keyword. ` # Outer fuction def func1(): var1 = 2 print("---ID of var1 in func1---") print(id(var1)) print(locals()) # Inner function def func2(): …
0
votes
0 answers

Scope of length of a given list

I'm learning about scopes of python variables: global, local, nonlocal, and built-in. And I'm having trouble with identifying the scope of following variable(var_5) in each line (1,2,3 7 4). list = [1,2,3,4,5] var_5 = len(list) - - - - -…
Indi
  • 421
  • 9
  • 28
0
votes
1 answer

Dynamically binding nonlocal variables

In order to bind a name to a variable from an outer-scope, we can use global or nonlocal keyword, but with the variable name known beforehand, e.g., x0 = 0 def f1(): x1 = 1 def f2(): x2 = 2 def f3(): x3 = 3 …