scope

Strange python closure variable access

Strange python closure variable access Question: While trying to implement a decorator with a closure I ran into a somewhat strange behavior, where a variable can be read, but if try to assign it later, it becomes undefined even before the assignment. def run_once(fn): to_run = True def decorated(*args, **kwargs): if to_run: print(to_run) #this access …

Total answers: 1

Another redeclared variable without usage in PyCharm

Another redeclared variable without usage in PyCharm Question: I’ve looked at several issues with this topic but I believe this is a new situation. I have this Python code in PyCharm 2022.2.2: res = [] for i in range(10): j = i for k in range(4): res.append(j) j = j + 1 On the line …

Total answers: 1

Confused about the scope of variable in python

Confused about the scope of variable in python Question: I was studying merge sort and I wrote this code def mergesort(a): #sorting algo #code is alright just confused about scope of variable if(len(a)>1): mid = len(a)//2 l = a[:mid] r = a[mid:] mergesort(l) mergesort(r) i=j=k=0 while(i<len(l) and j<len(r)): if(l[i]>r[j]): a[k] = r[j] j+=1 else: a[k] …

Total answers: 2

Dealing with scope in a recursive Python function

Dealing with scope in a recursive Python function Question: I have a JSON input file that looks like this: {"nodes": [ {"properties": { "id": "rootNode", "name": "Bertina Dunmore"}, "nodes": [ {"properties": { "id": 1, "name": "Gwenneth Rylett", "parent_id": "rootNode"}, "nodes": [ {"properties": { "id": 11, "name": "Joell Waye", "parent_id": 1}}, {"properties": { "id": 12, "name": …

Total answers: 3

How to define a free variable

How to define a free variable Question: I am currently reading the book Fluent Python – Luciano Ramalho (a great book IMO). In the chapter about decorators and closures, there is a following piece of code: def make_averager(): series = [] def averager(value): series.append(value) total = sum(series) return total/len(series) return averager avg = make_averager() So …

Total answers: 1

How to use a variable in __init__ in another function (Python)

How to use a variable in __init__ in another function (Python) Question: I am trying to use var1, which is the variable for a Tkinter Checkbutton in another function but I keep getting a Type Error which I am assuming is a result of var1 being a local scope in init. Normally, I would just …

Total answers: 1

How to understand global and local scopes in Python?

How to understand global and local scopes in Python? Question: I am a novice in Python and wondering the situation below. x = 1 def func(): print(x) x = 2 return x So I got the UnboundLocalError: local variable ‘x’ referenced before assignment. But if I right understand – Python read and execute code row …

Total answers: 1

Scoping issue with instance attributes and Class methods in tkinter/customtkinter

Scoping issue with instance attributes and Class methods in tkinter/customtkinter Question: My tkinter/customtkinter GUI uses multiple frames raised above one another. I have a two frames both containing switches that need to invert one another (ie when one is ‘ON’ the other must be ‘OFF’). Both switches are created as instance objects within the init …

Total answers: 1

Can a function and local variable have the same name?

Can a function and local variable have the same name? Question: Here’s an example of what I mean: def foo(): foo = 5 print(foo + 5) foo() # => 10 The code doesn’t produce any errors and runs perfectly. This contradicts the idea that variables and functions shouldn’t have the same name unless you overwrite …

Total answers: 7