Python: When and Why to Use __

Python, a high-level, interpreted programming language, is renowned for its simplicity and readability. One of its unique features is the use of double underscore (__) in certain contexts. But when should you use __ in Python? Let's delve into this, exploring the use of __ in names, special methods, and private attributes.

Master Python Loops: Break & Continue Made Simple πŸš€
Master Python Loops: Break & Continue Made Simple πŸš€

Before we dive in, let's clarify that Python doesn't have true private variables like some other languages. However, it uses naming conventions to indicate that an attribute should be treated as private. This is where __ comes into play.

Shortcut to learn Python.[Cheatsheet]
Shortcut to learn Python.[Cheatsheet]

Name Mangling with __

In Python, when you prefix a variable name with a double underscore, Python changes the name to include the class name. This is known as name mangling. It's a way to avoid naming conflicts and to indicate that the variable is intended to be private to the class.

10 Python Tricks Every Beginner Should Know
10 Python Tricks Every Beginner Should Know

For instance, consider the following code:

class MyClass:
    def __init__(self):
        self.__private_var = "I'm private"

obj = MyClass()
print(obj.__private_var)  # Raises AttributeError: 'MyClass' object has no attribute '__private_var'

As you can see, trying to access __private_var directly results in an AttributeError. This is because Python has mangled the name to __MyClass__private_var.

Python Notes
Python Notes

Unintended Access

While name mangling can't truly prevent access to the variable, it does make it more difficult. If you try to access the variable using the mangled name, you can retrieve its value:

print(obj._MyClass__private_var)  # Outputs: I'm private

However, this is not Python's intended use and is generally discouraged.

3 Python Mistakes Every Beginner Should Avoid | Python Programming Tips
3 Python Mistakes Every Beginner Should Avoid | Python Programming Tips

Use Cases

Name mangling is primarily used to avoid naming conflicts when multiple classes are used together. It also serves as a warning to developers that the variable is intended to be private and should not be accessed directly.

Special Methods with __

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

Python uses double underscores to denote special methods, also known as "magic methods" or "dunder methods". These methods provide functionality like object initialization (__init__), string representation (__str__), and operator overloading (__add__, __sub__, etc.).

For example, to create a custom class with a string representation, you would use the __str__ method:

Top 10 Python Mistakes Beginners Make
Top 10 Python Mistakes Beginners Make
Day 2 of becoming a python developer πŸπŸ’» Variables in python
Day 2 of becoming a python developer πŸπŸ’» Variables in python
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python
Python
the top 60 python project ideas for beginners to use in their web development projects
the top 60 python project ideas for beginners to use in their web development projects
Python Basics Cheat Sheet for Beginners | Learn Coding Step by Step
Python Basics Cheat Sheet for Beginners | Learn Coding Step by Step
Python Libraries Cheat Sheet | Learn What to Use & When
Python Libraries Cheat Sheet | Learn What to Use & When
Python Conditional Statements & Loops
Python Conditional Statements & Loops
Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
two buttons with the words python programming and don't
two buttons with the words python programming and don't
Python For Everything – Top Libraries & What They’re Used For
Python For Everything – Top Libraries & What They’re Used For
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
Python Input & Output Explained 🐍 | Beginner's Guide
Python Input & Output Explained 🐍 | Beginner's Guide
Python Default Arguments Explained
Python Default Arguments Explained
Important Methods in PYTHON
Important Methods in PYTHON
🐍 Python for Everything πŸš€ | Best Python Libraries & Tools Every Developer Should Learn
🐍 Python for Everything πŸš€ | Best Python Libraries & Tools Every Developer Should Learn
a table with different types of data structures
a table with different types of data structures
python for everything
python for everything
Important Methods in Python Part-II
Important Methods in Python Part-II
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables

class MyClass:
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return f"MyClass object with value {self.value}"

obj = MyClass(42)
print(obj)  # Outputs: MyClass object with value 42

Using __ in special methods allows Python to automatically call these methods in certain situations, making your code more intuitive and Pythonic.

__init__ Method

The __init__ method is a special method that Python calls when you create a new instance of a class. It's where you initialize the attributes of your class.

For instance, in the previous example, the __init__ method sets the value attribute of the MyClass object.

Operator Overloading

Python allows you to overload operators using special methods. For example, to add two instances of your class, you would define the __add__ method:

class MyClass:
    def __init__(self, value):
        self.value = value

    def __add__(self, other):
        return MyClass(self.value + other.value)

obj1 = MyClass(10)
obj2 = MyClass(20)
print(obj1 + obj2)  # Outputs: MyClass object with value 30

This allows you to use the + operator with your custom class, making your code more readable and intuitive.

In conclusion, while Python doesn't have true private variables, using __ in names provides a way to indicate that a variable is intended to be private. Moreover, using __ in special methods allows you to create powerful, intuitive, and Pythonic code. So, the next time you're wondering when to use __ in Python, remember: for private variables and special methods!