Python class NameError. Var is not defined [duplicate]
问题 This question already has answers here : Short description of the scoping rules? (9 answers) How can I access “static” class variables within class methods in Python? (6 answers) Closed 3 years ago . 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? 回答1: If you want your variables to be visible within the scope of your class functions, pass