How do you access a class variable in python?

The answer, in a 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 can be created dynamically. Therefore, you can do the following:

class Example(object):
    pass

Example.itsProblem = "problem"

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

print Example.itsProblem == e.itsSecondProblem 

prints

True

Therefore, 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, whether 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 (instance) 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 it 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 variables to set default values to object variables. Use __init__ for that.

Eventually, you will learn that Python classes are instances and therefore objects themselves, which gives new insight to understanding the above. Come back and read this again later, once you realize that.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In Python, we can define the variable outside the class, inside the class, and even inside the methods. Let’s see, how to use and access these variables throughout the program.

    Variable defined outside the class:

    The variables that are defined outside the class can be accessed by any class or any methods in the class by just writing the variable name.

    outVar = 'outside_class'    

    print("Outside_class1", outVar)

    class Geek:

        print("Outside_class2", outVar)

        def access_method(self):

            print("Outside_class3", outVar)

    uac = Geek()

    uac.access_method()

    class Another_Geek_class:

        print("Outside_class4", outVar) 

        def another_access_method(self):

            print("Outside_class5", outVar)

    uaac = Another_Geek_class()

    uaac.another_access_method()

    Output:

    Outside_class1 outside_class
    Outside_class2 outside_class
    Outside_class3 outside_class
    Outside_class4 outside_class
    Outside_class5 outside_class
    

     
    Variable defined inside the class:

    The variables that are defined inside the class but outside the method can be accessed within the class(all methods included) using the instance of a class. For Example – self.var_name.
    If you want to use that variable even outside the class, you must declared that variable as a global. Then the variable can be accessed using its name inside and outside the class and not using the instance of the class.

    class Geek:

        inVar = 'inside_class'

        print("Inside_class2", inVar)

        def access_method(self):

            print("Inside_class3", self.inVar)

    uac = Geek()

    uac.access_method()

    class another_Geek_class:

        print()

        def another_access_method(self):

            print()

    uaac = another_Geek_class()

    uaac.another_access_method()

    Output:

    Inside_class2 inside_class
    Inside_class3 inside_class
    

    The statements which are marked as error will produce an error upon execution as the variable is not accessible there.

     
    Variable defined inside the method:

    The variables that are defined inside the methods can be accessed within that method only by simply using the variable name. Example – var_name.
    If you want to use that variable outside the method or class, you have to declared that variable as a global.

    class Geek:

        print()

        def access_method(self):

            inVar = 'inside_method'

            print("Inside_method3", inVar)

    uac = Geek()

    uac.access_method()

    class AnotherGeek:

        print()

        def access_method(self):

            print()

    uaac = AnotherGeek()

    uaac.access_method()

    Output:

    Inside_method3 inside_method
    

    The statements which are marked as error will produce error upon execution as the variable is not accessible there.

    Summary:

    How do you access a class variable in python?


    How do you access a class variable?

    To access class variables, you use the same dot notation as with instance variables. To retrieve or change the value of the class variable, you can use either the instance or the name of the class on the left side of the dot.

    How do you use class variables?

    How to Use Variables within Classes.
    Using the This Keyword..
    Using Static as the Variable Type..
    Using State Variables..
    Assigning Props Values to the Variables..
    Const as a Variable..

    How do you access a variable in a function in a class?

    Use class_name. variable_name to access a class variable from a class method. Use class_name. variable_name to access a class variable with variable_name of class class_name from a class method.

    What is a class variable in Python?

    Class variable − A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are.