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
3
  • another possible duplicate stackoverflow.com/q/68645/1281433 Commented Jun 3, 2014 at 22:09
  • Nope, that doesn't involve functions. I've already read that question twice. Commented Jun 3, 2014 at 22:12
  • Did you see millerdev's answer there? Commented Jun 3, 2014 at 22:25

1 Answer 1

1

Try

class myClass:
    proxies = []
    def doIt(self):
        myClass.proxies.append(1)
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, that works. Is this the proper way and only way of doing it?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.