scope

Import a list from parent folder

Import a list from parent folder Question: parent_folder | – folder_1 |- a.py | – folder_2 |- b.py | – C.py If I have declared a list in c.py , how can I access it in a.py & b.py. No matter what I try, I keep running into ModuleNotFoundError. C.py def my_func(): my_list = ["value_1", …

Total answers: 1

scope strangeness in python class attributes

scope strangeness in python class attributes Question: Came across what seems to be a weird case of ‘spooky action at a distance’ using python class attributes. If I define X as: class X(): a = list() b = int def __init__(self, value): self.a.append(value) self.b = value Then instantiate: u = X(0) v = X(1) It …

Total answers: 1

Variable in generator function shadows name from outer scope

Variable in generator function shadows name from outer scope Question: I recently started to teach myself Python and currently work on generator functions. Here, I encountered a "scoping" issue with variable names inside of the generator shadowing names in outer scope. I did some research on this but could not come up with an explanation. …

Total answers: 1

`eval('bar')` causes a `NameError`, but `print(bar); eval('bar')` doesn't

`eval('bar')` causes a `NameError`, but `print(bar); eval('bar')` doesn't Question: The following code 1 def foo(): 2 def bar(): 3 return ‘blah’ 4 def baz(): 5 eval(‘bar’) 6 baz() 7 foo() causes a NameError — the entire error message is Traceback (most recent call last): File "main.py", line 7, in <module> foo() File "main.py", line 6, …

Total answers: 1

How can a function access variables that are not defined inside the function?

How can a function access variables that are not defined inside the function? Question: I recently started studying Python and I came across an example that I did not understand: def teste(): print(a, b) a = 5 b = 4 teste() # Outputs ‘5 4’ What is happening here? Is teste() able to access a …

Total answers: 1

How can a method directly access its class variables without using self?

How can a method directly access its class variables without using self? Question: I recently switched to Python from Java for development and is still not used to some of the implicitness of Python programming. I have a class which I have defined some class variables, how can I access the class variables within a …

Total answers: 2

Why is the global variable not showing the correct value

Why is the global variable not showing the correct value Question: The code is creating a random number from a low value and a high value supplied by the user. Why, when printing the value of the comp_num inside the function it returns the correct value but when printing it at the end of the …

Total answers: 2

Python, About UnboundLocalError occurring in the if or for statement

Python, About UnboundLocalError occurring in the if or for statement Question: An error occurred during the process of utilizing the notion API. I want to categorize the titles separately and make them look good, but my code can’t find the variable ‘title’. ERR: UnboundLocalError: local variable ‘title’ referenced before assignment import requests, json def read_database(database_id, …

Total answers: 3

My function is not defined properly, can anyone tell me why?

My function is not defined properly, can anyone tell me why? Question: I have a main function, and a getDogYears function that contains the variable dogYears. I have tried moving getDogYears above the main function, and I have tried initializing dogYears in the main function. I know dogYears is outside the scope of the main …

Total answers: 1