I’m trying to declare static variable and now my code is:
class StaticClass:
varA = 'hello'
@staticmethod
def staticMethod():
varA='bye'
Result of code below is hello. Why not ‘bye’ ?
StaticClass.staticMethod()
print StaticClass.varA
The code in
staticMethod()assigns the stringbyeto the local variablevarA, then returns, dropping the local variable. An assignment inside a function always creates local variables in Python. Astaticmethodin Python has no means of accessing the class at all — you need aclassmethodfor this purpose: