Fundamentals 11 min read

Understanding Python Underscore Naming Conventions and Name Mangling

This article explains the various meanings of single and double underscores in Python, outlines the five common underscore naming patterns, demonstrates how name mangling works for double‑leading underscores, and shows practical uses of the underscore as a throwaway variable and REPL placeholder.

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

The article introduces Python's underscore naming conventions, describing the purpose of single and double underscores (often called "dunders") and how they affect variable and method names, especially regarding name mangling in classes.

It enumerates five underscore patterns: _var (single leading), var_ (single trailing), __var (double leading), __var__ (double leading and trailing), and a solitary _ . Each pattern has a specific convention or technical effect.

A single leading underscore signals that a name is intended for internal use; the interpreter does not enforce any restriction. The article shows a Test class where self._bar can still be accessed directly, illustrating that the convention is merely a hint to programmers.

A single trailing underscore is used to avoid conflicts with Python keywords, such as defining a function def make_object(name, class_) when class is a reserved word.

Double leading underscores trigger name mangling: the interpreter rewrites the attribute name to include the class name (e.g., self.__baz becomes _Test__baz ) to prevent accidental overriding in subclasses. Examples with Test and ExtendedTest demonstrate how mangled names are stored and accessed.

Names that both start and end with double underscores (e.g., __init__ , __call__ ) are reserved for special Python methods and should be avoided for user‑defined identifiers to prevent clashes with future language features.

The solitary underscore _ also serves as a throwaway variable in loops, unpacking, and as the REPL’s placeholder for the most recent expression result, enabling quick interactive experimentation.

Finally, the article summarizes the five patterns, emphasizes the importance of following PEP 8 conventions, and encourages developers to recognize these naming patterns to write clearer, more maintainable Python code.

programmingnaming-conventionspep8Name ManglingUnderscores
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.