class Gui():
var = None
def refreshStats(args):
print(str(var))
clas = Gui()
clas.refreshStats()
Trace
File "sample.py", line 5, in refreshStats
print(str(var))
NameError: name 'var' is not defined.
Why?
If you want your variables to be visible within the scope of your class functions, pass self into every class function and use your class variables as self.var
such as this:
class Gui():
var = None
def refreshStats(self, args):
print(str(self.var))
clas = Gui()
clas.refreshStats()
If you want to use this as an instance variable (only for this instance of the class) as opposed to a class variable (shared across all instances of the class) you should be declaring it in the __init__
function:
class Gui():
def __init__(self):
self.var = None
def refreshStats(self, args):
print(str(self.var))
clas = Gui()
clas.refreshStats()