How can I access "static" class variables within methods in Python?
Instead of bar
use self.bar
or Foo.bar
. Assigning to Foo.bar
will create a static variable, and assigning to self.bar
will create an instance variable.
Define class method:
class Foo(object):
bar = 1
@classmethod
def bah(cls):
print cls.bar
Now if bah()
has to be instance method (i.e. have access to self), you can still directly access the class variable.
class Foo(object):
bar = 1
def bah(self):
print self.bar