Private Variables

Use name-mangling with double underscores to indicate private attributes.

PythonIntermediate
Python
# Program to demonstrate private variables (name mangling)

class Secret:
    def __init__(self, data):
        self.__data = data

    def reveal(self):
        print("Secret is:", self.__data)


s = Secret("hidden")
s.reveal()

# Direct access would fail: s.__data

Output

Secret is: hidden

Attributes starting with __ are name-mangled to _ClassName__attr, discouraging external access.