Private Variables in Python

Last Updated : 1 Nov, 2025

Python does not have true private variables. However, naming conventions and a mechanism called name mangling help indicate or simulate privacy. Underscores (_ and __) are commonly used to signal how an attribute or method should be accessed.

Name Mangling in Python

Name mangling automatically changes the names of attributes starting with double underscores (__) to include the class name. This helps avoid accidental overriding in subclasses.

Example: The following example shows how methods with double underscores in parent and child classes are kept separate.

Python
class A:
    def __hidden(self):
        print("Inside class A")

class B(A):
    def __hidden(self):
        print("Inside class B")

obj = B()
obj._A__hidden() 

Output
Inside class A

Explanation:

  • __hidden in A becomes _A__hidden.
  • B also has its own _B__hidden.
  • This avoids conflicts between base and subclass methods.

_Single Leading Underscores

A single leading underscore (_var) indicates that an attribute is for internal use only. It’s just a convention Python doesn’t enforce it.

Example: This example demonstrates how a variable with a single underscore is used internally within the class.

Python
class Example:
    def __init__(self):
        self._value = "Internal data"

    def get_value(self):
        return self._value

obj = Example()
print(obj.get_value())

Output
Internal data

Explanation:

  • _value is a non-public variable meant for internal logic.
  • Other developers should treat it as private, even though it’s accessible.

__Double Leading Underscore(__var)

A double leading underscore triggers name mangling, which helps avoid accidental name clashes in subclasses. It changes the attribute name internally to _ClassName__var.

Example: This example shows how name mangling prevents a subclass from overriding a base class method.

Python
class Base:
    def __secret(self):
        print("Base method")

class Child(Base):
    def __secret(self):
        print("Child method")

obj = Child()
obj._Base__secret()

Output
Base method

Explanation:

  • Base.__secret becomes _Base__secret.
  • Child.__secret becomes _Child__secret.
  • This prevents accidental overriding between parent and child classes.

__Double Leading and Double Trailing Underscores__

Identifiers that begin and end with double underscores are called special (or magic) methods. They are not used for making variables private but for defining Python’s built-in behaviors - such as object creation, string conversion, or operator overloading.

Here are some of the most common double underscore (dunder) methods in Python: __init__(), __str__(), __len__(), __add__(), __eq__() etc.

Example: This example shows how dunder methods define built-in behaviors like addition and length.

Python
class Numbers:
    def __init__(self, data):
        self.data = data

    def __len__(self):              
        return len(self.data)

    def __add__(self, other):       
        return Numbers(self.data + other.data)

nums1 = Numbers([1, 2, 3])
nums2 = Numbers([4, 5])
result = nums1 + nums2

print(len(nums1))   
print(result.data)

Output
3
[1, 2, 3, 4, 5]

Explanation:

  • __len__() defines behavior when len() is called.
  • __add__() defines behavior when + is used.
  • These dunder (double-underscore) methods let custom classes behave like Python’s built-in types.
Comment