add a public static property to a class in python

To add a public static property to a class in Python, we can use the class decorator @staticmethod along with the built-in @property decorator.

Here is an example:

main.py
class MyClass:
    _my_static_property = "Hello, World!" #private variable
    
    @staticmethod
    @property
    def my_static_property():
        return MyClass._my_static_property
185 chars
8 lines

In the above example, we added a private static variable _my_static_property to the class MyClass. We then defined a static method my_static_property and decorated it with @staticmethod and @property. This allows us to access the private static variable as a public static property.

We can then access the public static property as follows:

main.py
print(MyClass.my_static_property) # Output: Hello, World!
58 chars
2 lines

gistlibby LogSnag