Skip to content

Python static object

  • by

In Python, there is no concept of a “static object” in the same way it exists in some other programming languages like Java or C++. However, you can achieve similar functionality using class attributes or module-level variables.

Class-level attributes: In Python, you can define class attributes, which are shared by all instances of a class. These attributes are similar to static variables in other languages.

class MyClass:
    static_var = 10

    def __init__(self):
        self.instance_var = 20

print(MyClass.static_var)  # Accessing the static variable

obj1 = MyClass()
obj2 = MyClass()

print(obj1.static_var)  # Accessing the static variable through an instance
print(obj2.static_var)  # Accessing the static variable through another instance

obj1.static_var = 30  # Modifying the static variable through an instance

print(obj1.static_var)  # Accessing the modified static variable through an instance
print(obj2.static_var)  # Accessing the static variable through another instance (not affected by the modification)

Module-level variables: In Python, you can define variables at the module level, outside of any class or function. These variables can be accessed and modified from anywhere within the module, similar to static variables in other languages.

# mymodule.py
static_var = 10

def my_function():
    global static_var
    static_var = 20

# main.py
import mymodule

print(mymodule.static_var)  # Accessing the static variable

mymodule.my_function()  # Modifying the static variable

print(mymodule.static_var)  # Accessing the modified static variable

Note: module-level variables are shared across all instances of a class if the class is defined within the module.

Python static object example

Here’s an example of a class with class methods that can be used to work with a “static object” in Python:

class StaticObject:
    static_variable = 0

    @classmethod
    def increment(cls):
        cls.static_variable += 1

    @classmethod
    def decrement(cls):
        cls.static_variable -= 1

    @classmethod
    def get_value(cls):
        return cls.static_variable


StaticObject.increment()
StaticObject.increment()
StaticObject.increment()

print(StaticObject.get_value())

StaticObject.decrement()

print(StaticObject.get_value())

Output:

Python static object

In this example, the StaticObject class has a static_variable that is shared among all instances of the class. The increment() and decrement() class methods modify the static_variable, and the get_value() class method retrieves its value.

By calling the class methods directly on the StaticObject class, you can work with the “static object” in a similar manner as you would with static methods in other languages.

Do comment if you have any doubts or suggestions on this Python object topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading