Private Variables in Python
Use name-mangling with double underscores to indicate private attributes.
IntermediateObject-Oriented ProgramsExample 17 of 25
private-variables.py
Run in browser1# Program to demonstrate private variables (name mangling)23class Secret:4 def __init__(self, data):5 self.__data = data67 def reveal(self):8 print("Secret is:", self.__data)910s = Secret("hidden")11s.reveal()1213# Direct access would fail: s.__data
Output
Secret is: hidden
What's going on
Attributes starting with __ are name-mangled to _ClassName__attr, discouraging external access.