You are currently viewing Static or Class Variables in Python?

Static class variables in Python are a way to share data between instances of a class and are commonly used in other programming languages as well. In this article, we will explore the concept of static class variables in Python with examples.

Advertisements

The quick answer to this question is, Yes, static class variables are possible in Python. They are defined outside of any method in the class definition and are shared among all instances of the class. We will see explanations along with examples in upcoming sections.

1. Static Class Variables in Python

In Python, a class is a blueprint for creating objects, which can have both instance and class variables. Instance variables are unique to each instance of a class and you have to define them within the constructor method.

On the other hand, class variables are shared among all instances of the class and are defined outside of any method in the class definition. These class variables are also known as static class variables.

Unlike other programming languages, static class variables in Python are not truly “static” as their value can be changed by any instance of the class.


# Create a class
class MyClass:
    static_var = "static class variable" # Class or Static Variable

    def __init__(self, instance_var):
        self.instance_var = instance_var # Instance Variable

# Accessing using the class name
print(MyClass.static_var) 

# Output: 
# static class variable

# Accessing using an instance of the class
obj1 = MyClass("Instance 1")
print(obj1.static_var) 

# Output: 
# static class variable

2. Is it Possible to Create Static Class Variables in Python?

In Python, static class variables are possible and can be defined using the class name or the cls argument within class methods. There are several ways to define static class variables in Python, including using the @staticmethod decorator, the @classmethod decorator, and the @property decorator.

2.1 @staticmethod Decorator for Static Class Variables

The first method is to use @staticmethod decorator. You can use it to define a static method within a class. This method can be accessed using the class name and does not require an instance of the class to be created. A static method can be used to define a static class variable and can be accessed using the class name.


class MyClass:
    static_var = None

    @staticmethod
    def set_static_var(value):
        MyClass.static_var = value

MyClass.set_static_var(10)
print(MyClass.static_var)  

# Output: 
# 10

static_var is a static class variable defined within the MyClass class.

2.2 @classmethod Decorator for Static Class Variables

In the same way, we can use the @classmethod decorator can be to define a class method within a class. This method takes the class itself as an argument and we can use it to access or modify static class variables. You can use the class method to define a static class variable which you can access using the class name.


class MyClass:
    static_var = None

    @classmethod
    def set_static_var(cls, value):
        cls.static_var = value

MyClass.set_static_var(20)
print(MyClass.static_var) 
 
# Output: 
# 20

2.3 @property Decorator for Static Class Variables

You can also use the @property decorator to define a property within a class. We can access this property like an attribute and can use it, to define a static class variable. And now, we can access this property using the class name.


class MyClass:
    _static_var = None

    @property
    def static_var(self):
        return MyClass._static_var

    @staticmethod
    def set_static_var(value):
        MyClass._static_var = value

MyClass.set_static_var(30)
print(MyClass.static_var)  

# Output: 
# 30

3. Access Static Variables from inside a Class?

A Static variable can be accessed from inside a class using the cls keyword. The cls keyword is the first argument of a class method. This argument refers to the class itself and is used to access class-level attributes such as static variables and other class methods.

Inside a class method, the cls argument is treated like a regular parameter, but it is conventionally named cls to make it clear that it refers to the class.

See the following code example:


# Access Static Variables from inside a Class?
class MyClass:
    # Static class Variable
    my_static_var = 78

    @classmethod
    def print_static_var(cls):
        # Accessing inside a class
        print(cls.my_static_var)

4. Access Static Variables from Outside a Class?

The value of a static variable is the same for all instances of the class. We can access a static variable from outside a class using either the class name or an instance of the class.

To access a static variable using the class name, you simply reference the variable using the class name. See the below example.


# Access Static Variables from Outside a Class?
class MyClass:
    my_static_var = 78

print(MyClass.my_static_var)

# Output : 78

While using an instance of the class instead of the class name, you can reference the variable using the instance variable name.


obj = MyClass()
print(obj.my_static_var)

# Output : 78

We first created the instance of the class and then we use the dot operator to access the static variable from outside the class. In both cases, the value of the static variable is the same and is shared by all instances of the class.

While accessing a static variable from inside or outside, keep in mind if you try to modify a static variable using the class name, you’ll actually be modifying the value of the variable for all instances of the class.


class MyClass:
    my_static_var = 78

MyClass.my_static_var = 24
print(MyClass.my_static_var)  

# Output: 24

While the case is different when you access it with the instance of the class. Python will create an instance variable with the same name as the static variable, which is only visible to that instance. And it will not be changed for all instances.


class MyClass:
    my_static_var = 78

obj = MyClass()
obj.my_static_var = 24
print(MyClass.my_static_var)  

# Output: 78

print(obj.my_static_var)   
   
# Output: 24

5. Summary and Conclusion

We have learned how Static class variables in Python are possible. While Python doesn’t have a keyword for declaring static variables, you can implement it, using different techniques such as the @staticmethod, @classmethod, and @property decorators. If you have questions, leave them in the comment section.

Happy Coding!