views:

99

answers:

2
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.

+2  A: 

You are declaring a local variable, not a class variable. To set an instance variable (attribute), use

class Example(object):
    def the_example(self):
        self.itsProblem = "problem"  # <-- remember the 'self.'

theExample = Example()
theExample.the_example()
print(theExample.itsProblem)

To set a class variable (a.k.a. static member), use

class Example(object):
    def the_example(self):
        Example.itsProblem = "problem"
        # or, type(self).itsProblem = "problem"
        # depending what you want to do when the class is derived.
KennyTM
Except that class variables are declared *once* at *class-level*. Your way will reset it upon every instanciation, this really isn't what you want. Also see http://stackoverflow.com/questions/2709821/python-self-explained for the rationale behind explicit self.
delnan
+10  A: 

The anwser, in few words

In your example, itsProblem is a local variable.

Your must use self to set and get instance variables. You can set it in the __init__ method. Then your code would be:

class Example(object):
    def __init__(self):
        self.itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)

But if you want a true class variable, then, use the class name directly:

class Example(object):
    itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)
print (Example.itsProblem)

But be careful with this one, as theExample.itsProblem is automatically set to be equal to Example.itsProblem, but is not the same variable at all and can be changed independently.

Some explanations

In Python, variables car be created dynamically. Therefor, you can do that:

class Example(object):
    pass

Example.itsProblem = "problem"

e = Example()
e.itsSecondProblem = "problem"

print Example.itsProblem == e.itsSecondProblem 

Therefor, that's exactly what you do with the previous examples.

Indeed, in Python we use self as this, but it's a bit more than that. Self is the the first argument to any object method because the first argument is always the object reference. This is automatic, wherever you call it self or not.

Which means you can do:

class Example(object):
    def __init__(self):
        self.itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)

Or:

class Example(object):
    def __init__(my_super_self):
        my_super_self.itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)

It's exactly the same. The first argument of ANY object method is the current object, we only call it self as a convention. And you add just a variable to this object, the same way you would do it from outside.

Now, about the class variables. When you do:

class Example(object):
    itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)

You'll notice we first set a class variable, then we access an object variable. We never set this object variable but it works, how is that possible?

Well, Python tries to get first the object variable, but if he can't find it, will give you the class variable. Warning: the class variable is shared among instances, and the object variable is not.

As a conclusion, never use class variable to set default value to object variables. Use __init__ for that.

Eventually, you will learn that Python classes are instances and therefor object them-self, which give new lights to understand the above. Come back reading it again later, once you realize that.

e-satis
+1 for __init__. That's what it's for.
Abizern
you say: "theExample.itsProblem is automatically set to be equal to Example.itsProblem, but is not the same variable at all and can be changed independently" - but that is not quite right, and your phrase is misleading. I trust you know what is going on there, so I's suggest rephrasing that: "it _is_ the same variable, but it can be rebinded independently for each object".
jsbueno
Yes, but binding is a concept somebody for an other programming language such as Java or C (as I suspect the OP is) that is completly unknown. Then I would I to explain what binding is, then late binding, then the problem with references on mutable objects. It would be too long. I think sometime you must sacrifice precision on the altar of understanding.
e-satis