call a private static property on an instance of a class in python

To call a private static property on an instance of a class in Python, you can use the class name instead of the instance name to access the static property. Additionally, you can use the double underscore prefix to indicate that the property is private.

Here's an example:

main.py
class MyClass:
    __my_private_static_property = "hello"

    def __init__(self):
        pass

my_instance = MyClass()
print(MyClass.__my_private_static_property)
165 chars
9 lines

In this example, __my_private_static_property is a private static property on the MyClass class, indicated by the double underscore prefix. To access the property, we use the class name MyClass instead of my_instance. The output of the code will be:

main.py
hello
6 chars
2 lines

Note that this is not the recommended way to access private properties, as it goes against the principle of encapsulation. It is best to avoid accessing private properties directly and instead use public methods to access and modify them.

gistlibby LogSnag