Contents of file_one.py:
from file_two import *
def function_one():
my_list = []
function_two()
function_one()
Contents of file_two.py:
def function_two():
my_list.append(1)
print my_list
Error on running 'file_one.py':
NameError: global name 'my_list' is not defined
The above example is a simplified version of what i am actually trying to implement however it produces the same error and i believe it replicates the same logic.
In the actual implementation, as a newbie, i was importing a file full of functions (file_two.py) into file_one.py and then just using the function name in file_one.py when i wanted to call it.
This worked fine until i had to use a variable from a function in file_one.py in a function in file_two.py (as demonstrated above). Hence my need to understand classes.
This seems to be a very succinct description and implementation of classes:
https://stackoverflow.com/a/10139935/1063287
However i don't (that i can currently see) need to use a function from a class, just a variable.
I get stuck here thinking whether i just need to create a simpler class file (without functions) and if so:
- what would be required to be in it (in regards to syntax etc)?
- and how would i reference the variables in it from elsewhere (again in regards to syntax etc)?