Home / News / Single and Double Underscores in Python Names

Single and Double Underscores in Python Names

Python has a few naming conventions that are based on using either a single or double underscore character (_). These conventions allow you to differentiate between public and non-public names in APIs, write subclasses safely, prevent name collisions, and more.

Following these conventions makes your code look more Pythonic and consistent to other developers. This skill is especially helpful when you’re working on collaborative projects.

By the end of this tutorial, you’ll understand that:

  • Underscores in Python names indicate intent: a single leading underscore signals a non-public name, a single trailing underscore helps avoid naming conflicts, and a double leading underscore triggers name mangling for class attributes and methods.
  • Python doesn’t enforce public or private names with access restrictions. It relies on naming conventions, where public names have no underscores and non-public names start with a single underscore.
  • Python’s name mangling automatically renames attributes or methods with double leading underscores by prefixing them with the class name, helping you avoid accidental overrides in subclasses.
  • Double leading and trailing underscores—known as dunders—denote special methods or attributes, such as .__init__(), .__len__(), and __name__, which Python uses to support internal behaviors.

You’ll explore practical examples of these naming conventions, learn when and why to use each one, and understand their effects on code readability, API design, and inheritance.

Get Your Code: Click here to download the free sample code that shows you how to use single and double underscores in Python names.

Take the Quiz: Test your knowledge with our interactive “Single and Double Underscores in Python Names” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Single and Double Underscores in Python Names

In this quiz, you’ll test your understanding of the use of single and double underscores in Python names. This knowledge will help you differentiate between public and non-public names, avoid name clashes, and write code that looks Pythonic and consistent.

Public Interfaces and Naming Conventions in Python

As a Python programmer, you’ll frequently work with public interfaces, known as application programming interfaces (APIs). An API is a type of programming interface that offers a service to other parts of a program or other programs.

For example, the Python standard library has many modules and packages that provide certain services. To use these modules and packages, you need to access their public components, such as classes, functions, variables, constants, and modules. All these objects are part of the module or package’s public interface. They’re available for you to use directly in your code.

However, many of these packages and modules define objects that aren’t intended for direct access. These objects are meant for internal use within the specific package or module and aren’t part of its public interface.

In the context of object-oriented programming, languages like C++ and Java have the notion of public and private methods and attributes—jointly called members. In these languages, you can use these types of class members as follows:

  • Public: You can use them in your own code or client code.
  • Private: You can use them only from inside the defining class and its subclasses.

These languages have specific keywords and syntax to define public and private members in their classes. Once you declare a member as private, you can’t use it outside the class because the language restricts access. So, private members aren’t part of the class’s public interface, and there’s no way to access them.

In contrast, Python doesn’t have the notion of public and private members. It has neither dedicated keywords nor syntax for defining them. Therefore, you can always access the members of a Python class.

If Python doesn’t have a specific syntax to define when an object is part of a public interface, then how do you tell your users that they can or can’t use a given class, method, function, variable, constant, or even module in their code?

To approach this question, the Python community has a well-established naming convention:

If a name starts with a letter in uppercase or lowercase, then you should consider that name public and, therefore, part of the code’s API. In contrast, if a name starts with an underscore character (_), then you should consider that name non-public, meaning it’s not a part of the public API.

You should observe these naming conventions to explicitly indicate whether other developers should directly use your variables, constants, functions, methods, and modules in external code.

Note: This naming convention doesn’t restrict access to objects. It only signals to other developers how the code is intended to be used. Because of this, Python programmers avoid the terms public and private. Instead, they distinguish between public and non-public (internal) names.

The Python community uses the underscore character (_) as part of other naming conventions. Here’s a summary of what PEP 8 says about using this character in names:

Convention Example Meaning
Single leading underscore _variable Indicates that the name is meant for internal use only
Single trailing underscore class_ Avoids naming conflicts with Python keywords and built-in names
Double leading underscore __attribute Triggers name mangling in the context of Python classes
Double leading and trailing underscore __name__ Indicates special attributes and methods that Python provides
Single underscore _ Indicates a temporary or throwaway variable

Note that only two of these naming conventions enforce specific Python behaviors. Using double leading underscores triggers name mangling in Python classes. You’ll learn more about this behavior in the section on name mangling.

Additionally, those names with double leading and trailing underscores that are listed in the Python data model trigger internal behaviors in specific contexts. You’ll also learn more about this topic in the section on dunder names in Python.

Note: Python also treats a single underscore (_) as a soft keyword within matchcase statements. You’ll learn more about this later.

Read the full article at https://realpython.com/python-double-underscore/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Tagged: