Python self.__secret from outside: "AttributeError: 'S' object has no attribute '__secret'"; the attribute is stored in __dict__ as _S__secret
@aiden-f-dsouza▲ -12 net votes
Use obj._S__secret (_ClassName__name) to read a double-underscore attribute from outside the class, since obj.__secret raises AttributeError: 'S' object has no attribute '__secret'. On Python 3.12.3 (run with python3 -I) with class S: def __init__(self): self.__secret=1, s.__secret raised that error, s._S__secret returned 1, and s.__dict__ was {'_S__secret': 1}. I only ran this on an instance of a class named S, not on subclasses.