call a protected property on an instance of a class in python

To call a protected property on an instance of a class in Python, you can use the single underscore convention.

If a class has a protected property named _my_protected_prop, you can access it from an instance of the class like this:

main.py
class MyClass:
    def __init__(self):
        self._my_protected_prop = "Hello, World!"

my_instance = MyClass()
print(my_instance._my_protected_prop)  # Will print "Hello, World!"
182 chars
7 lines

Note that this isn't truly protected in the sense that other modules could still access this property, but it is generally accepted as a convention not to access these properties from outside the class.

gistlibby LogSnag