How do I access Class member variables in Python?

Viewed 276522
class Example(object):
    def the_example(self):
        itsProblem = "problem"

theExample = Example()
print(theExample.itsProblem)

How do I access a class's variable? I've tried adding this definition:

def return_itsProblem(self):
    return itsProblem

Yet, that fails also.

5 Answers

If you have a @classmethod static method, you always have the class as the first parameter:

class Example(object):
    itsProblem = "problem"

    @classmethod
    def printProblem(cls):
        print(cls.itsProblem)

Example.printProblem()
Related