Fundamentals 6 min read

Decoding Python’s Underscore Conventions: From Throwaway Variables to Dunder Methods

This article explains the meaning behind Python’s various underscore patterns—single underscores for throwaway variables, leading underscores for internal use, double leading underscores for name‑mangling, dunder methods for special behavior, and trailing underscores to avoid keyword conflicts—helping developers write clearer, more idiomatic code.

Code Mala Tang
Code Mala Tang
Code Mala Tang
Decoding Python’s Underscore Conventions: From Throwaway Variables to Dunder Methods

If you have ever read or written Python code, you have probably encountered various forms of underscores such as _, _var, __name, __init__, or class_. Most people either ignore them or think they are merely decorative, but they actually carry specific meanings that affect visibility, naming conventions, and how Python interacts with your code behind the scenes.

1. _ (single underscore)

In Python, a single underscore is typically used as a “throwaway” variable, a placeholder for values that are required syntactically but will not be used.

For example, in a for loop where you only care about the number of iterations, you can write:

for _ in range(5):
    ...

2. _var (single leading underscore)

Python does not have true private variables, but a leading underscore signals a convention that the variable is intended for internal use only and should not be touched by external code.

Example:

class MyClass:
    def __init__(self):
        self._hidden = "Shhh!"

Here _hidden is considered private by convention; while it can still be accessed from outside, well‑written code respects the underscore.

3. __var (double leading underscore)

When a variable or method name starts with double underscores, Python performs name‑mangling, internally rewriting the name to include the class name. This helps avoid accidental access or naming conflicts in subclasses.

Example:

class MySecret:
    def __init__(self):
        self.__password = "12345"

The attribute __password is mangled to something like _MySecret__password. Although not a security feature—knowing the mangled name lets you access it—it reduces the chance of accidental clashes.

ms = MySecret()
print(ms._MySecret__password)

4. __var__ (double leading and trailing underscores)

These are called “dunder” names (short for double‑underscore) and denote special methods or attributes provided by Python, such as: __init__ → constructor method __str__ → string representation __len__ → length method

Defining a __str__ method, for instance, makes object printing more readable:

class Book:
    def __init__(self, title):
        self.title = title
    def __str__(self):
        return f"Book: {self.title}"

print(Book("1984"))

Without __str__, printing the object would yield a less friendly representation like <__main__.Book object at 0x...>.

Important tip: Unless you are building Python internals or advanced libraries, avoid creating your own dunder names such as __myvar__ . Stick to the ones provided by Python.

5. var_ (trailing underscore)

If a variable name would clash with a Python keyword (e.g., class, def, lambda), the convention is to add a trailing underscore.

Example:

class_ = "Physics"
print(class_)

This is perfectly valid and clearly conveys the intended meaning.

Summary

Now you know the significance of the various underscore patterns in Python. They may seem trivial, but they carry substantial meaning. Pay attention to them in code—they often convey important intent.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythoncode styleNaming Conventionunderscoredunder
Code Mala Tang
Written by

Code Mala Tang

Read source code together, write articles together, and enjoy spicy hot pot together.

0 followers
Reader feedback

How this landed with the community

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.