add a protected static property to a class in python

To add a protected static property to a Python class, we can define a class-level property using the @staticmethod decorator, and prepend the property name with two underscores to indicate protected access.

Here's an example:

main.py
class MyClass:
    __my_prop = "protected"

    @staticmethod
    def my_static_method():
        print("Static method")

    @staticmethod
    def get_protected_prop():
        return MyClass.__my_prop
203 chars
11 lines

In this example, __my_prop is a protected static property of the MyClass class, which can be accessed within the class using MyClass.__my_prop. The get_protected_prop() static method provides a way to access this property from outside the class.

Note that while this syntax provides a way to achieve protected access, there is no true encapsulation in Python, as any attribute can be accessed and modified from outside the class if desired.

gistlibby LogSnag