Fundamentals 11 min read

Understanding Python Underscore Naming Conventions and Name Mangling

This article explains the meaning and conventions of single and double leading, trailing, and surrounding underscores in Python identifiers, shows how name mangling works, and provides practical code examples illustrating their effects on classes, modules, and REPL usage.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding Python Underscore Naming Conventions and Name Mangling

1. Single Leading Underscore _var

A single leading underscore is a convention that signals an internal‑use variable or method; Python does not enforce any restriction, but it does affect "from module import *" by excluding such names unless they appear in __all__ .

<code>class Test:
    def __init__(self):
        self.foo = 11
        self._bar = 23

>>> t = Test()
>>> t.foo
11
>>> t._bar
23</code>

2. Single Trailing Underscore var_

When a desired identifier collides with a Python keyword, appending a trailing underscore resolves the conflict while keeping the name readable.

<code>def make_object(name, class_):
    pass</code>

3. Double Leading Underscore __var

Names that start with two underscores trigger name mangling: the interpreter rewrites the attribute name to _ClassName__var to avoid accidental overriding in subclasses.

<code>class Test:
    def __init__(self):
        self.__baz = 23

>>> t = Test()
>>> dir(t)
['_Test__baz', '__class__', '__delattr__', ...]
</code>

When a subclass defines its own __baz , the original attribute remains mangled as _Test__baz while the subclass gets _SubClass__baz .

<code>class ExtendedTest(Test):
    def __init__(self):
        super().__init__()
        self.__baz = 'overridden'

>>> et = ExtendedTest()
>>> et._ExtendedTest__baz
'overridden'
>>> et._Test__baz
23</code>

4. Double Leading and Trailing Underscore __var__

Identifiers that both start and end with double underscores are reserved for special Python methods ("dunder" methods) such as __init__ or __call__ . They are not subject to name mangling.

<code>class PrefixPostfixTest:
    def __init__(self):
        self.__bam_ = 42

>>> PrefixPostfixTest().__bam_
42</code>

5. Single Underscore _

A solitary underscore is often used as a throw‑away variable in loops, unpacking, or as the REPL’s placeholder for the last evaluated expression.

<code>for _ in range(3):
    print('Hello')

car = ('red', 'auto', 12, 3812.4)
color, _, _, mileage = car

>>> _
12
</code>

The article concludes with a quick reference table summarizing the five underscore patterns and their typical meanings.

code examplesnaming-conventionsunderscorepep8Name Mangling
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.