0

I'm trying to get this code to run. How do I access the class's static variable inside a function? Can't believe I can't find a similar question.

class myClass:
    proxies = []
    def doIt(self):
        proxies.append(1)

theClass = myClass()
print theClass.proxies
print myClass.proxies
theClass.doIt()
print theClass.proxies
print myClass.proxies
User
  • 23,729
  • 38
  • 124
  • 207

1 Answers1

1

Try

class myClass:
    proxies = []
    def doIt(self):
        myClass.proxies.append(1)
S. Dixon
  • 842
  • 1
  • 12
  • 26
  • Awesome, that works. Is this the proper way and only way of doing it? – User Jun 03 '14 at 22:13
  • Well, in some cases, `self` keyword can also be used to reference the static member. However, if you assign `self.proxies` then the object will have both a static and a member variable called `proxies`. It is also possible to have a local variable called `proxies` in the `doIt` function as well. – S. Dixon Jun 03 '14 at 22:19