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

How to check if a variable with a given name is nonlocal?

Given a stack frame and a variable name, how do I tell if that variable is nonlocal? Example: import inspect def is_nonlocal(frame, varname): # How do I implement this? return varname not in frame.f_locals # This does NOT work def f(): …
user541686
  • 205,094
  • 128
  • 528
  • 886
3
votes
0 answers

State variables in Python: when to choose classes, nonlocal variables, or function attributes

There are (at least) three different ways to track state information for functions in python (examples presented without any meaningful way to use the state information, obviously): OOP Classes: class foo: def __init__(self, start): …
DrMag
  • 39
  • 1
  • 3
3
votes
0 answers

Using the variable type annotation as a placeholder in a nonlocal scope?

Since PEP 526 -- Syntax for Variable Annotations was approved, in Python 3.6+ it is possible to provide type hint information in the form x: int, also the PEP says "However, annotating a local variable will cause the interpreter to always make it…
godaygo
  • 2,215
  • 2
  • 16
  • 33
3
votes
0 answers

How to get complete namespace with Python's code.interact?

How can I get access to the complete namespace, including nonlocal names, when for instance doing code.interact in Python (v. 3.6)? Often suggested is the following solution or some equivalent: code.interact(local={**globals(), **locals()}) This…
Miker
  • 31
  • 1
2
votes
1 answer

Python inner function sets vs nonlocal

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() …
adinutzyc21
  • 1,538
  • 1
  • 11
  • 23
2
votes
1 answer

python nonlocal - why sometimes need it and sometimes not

I know why we need to declare nonlocal in Python and am a bit confused about the following example. Without nonlocal records in line 276, records in line 277 is not defined. However, records in line 289 can be used without any error. And the…
uoay
  • 306
  • 2
  • 13
2
votes
2 answers

Python: How to allow ‘inner function’ to change nonlocal variables in multiple ‘outer functions’

Suppose I have a function, which has a large section of code repeated in various places within the function, I can do the following: def foo(): def bar(): # do some stuff bar() # do some other stuff bar() I can ‘read’ the…
tim-mccurrach
  • 6,395
  • 4
  • 23
  • 41
2
votes
1 answer

Trying to use variables outside of a for loop gives a SyntaxError: no binding for nonlocal 'max_' found

def min_diff(arry_): max_ =0 temp_ =0 for i in arry_: nonlocal max_ nonlocal temp_ if i > max_: nonlocal max_ nonlocal temp_ temp_ = max_ max_ =i return max_-temp_ I want to use max_ and…
Shailab Singh
  • 89
  • 1
  • 1
  • 9
2
votes
2 answers

Understanding nonlocal in Python 3

I am trying to understand Python 3 variable scoping and nonlocal. Consider the following function (it is just an example): def build_property(something): def deco(func): def getter(self): return getattr(self, something) …
Hernan
  • 5,811
  • 10
  • 51
  • 86
1
vote
1 answer

Reuse of an inner function in Python

I'm trying to figure out how to use a Python 3 function (using nonlocal variables) within more than one other function without defining it all over again. Here's a very simplified example of what I mean: def inner(airplane): nonlocal var if…
Russianspi
  • 23
  • 6
1
vote
0 answers

Python: How to define functions in loops using outer-scope variables?

I want to define a function inside a loop using and changing variables which have been defined within the same loop iteration. I would expect that nonlocal is just what i need here, but if i implement it like this for i in range(5): obj =…
1
vote
1 answer

Write a function that takes in no arguments and returns two functions, prepend and get,

Write a function that takes in no arguments and returns two functions, prepend and get, which represent the “add to front of list” and “get the ith item” operations, respectively. Do not use any python built-in data structures like lists or…
Proteus Yi
  • 53
  • 5
1
vote
1 answer

How can the most inner function access the non-local variable in the most outer function in Python?

inner() can access the non-local variable x in middle() with nonlocal x: def outer(): x = 0 def middle(): x = 5 # <- Here def inner(): nonlocal x # Here x += 1 print(x) # 6 inner() …
1
vote
1 answer

Does 'nonlocal' variable always inherit from the outer loop, even in recursive calls?

I have written some code and recently came across the need for 'nonlocal' when writing functions of functions (came across this when dealing with recursion). For example: def swapPairs(head): head = None def helper(node): nonlocal…
1
vote
0 answers

Use python nonlocal varibles to store the output of a recursion VS return the recursion directly

I'm trying to design a function that produce the names of wizards who be in the same house as their parent, by passing in some Wizard that come together formed a family tree, as stated in the docstrings. ## Data Definitions: ## Wizard is (name,…
underhill
  • 35
  • 4