Single and Double Underscores in Python Names :
by:
blow post content copied from Real Python
click here to view original post
Python has a few important 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 safe classes for subclassing purposes, avoid name clashes, and more.
Following and respecting these conventions allows you to write code that looks Pythonic and consistent in the eyes of other Python developers. This skill is especially useful when you’re writing code that’s intended for other developers to work with.
In this tutorial, you’ll:
- Learn about Python naming conventions that rely on using underscores (
_
) - Differentiate public and non-public names by using a single leading underscore
- Use double leading underscores to leverage name mangling in Python classes
- Explore other common uses of underscores in Python names
To get the most out of this tutorial, you should be familiar with Python variables, constants, functions, modules, and especially classes and object-oriented programming.
Get Your Code: Click here to download the free sample code that shows you how to use single and double underscores in Python names.
Public Interfaces and Naming Conventions in Python
As a Python programmer, you’ll frequently work with public interfaces, or application programming interfaces (API). 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 for the internal use of the specific package or module only. They’re not 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. 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. So, 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 well-established naming conventions. 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 that the naming conventions don’t restrict access to objects. They’re just a warning to other developers using your code. Because of this, Python doesn’t use the terms public and private. Instead, it uses the terms public and non-public.
In Python, 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 code’s API.
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 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 gives special treatment to the single underscore (_
), but only within match
… case
statements. You’ll learn more about this later.
In the following section, you’ll learn more about using a single leading underscore when naming your Python objects.
Single Leading Underscore in Python Names
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 ]
November 29, 2023 at 07:30PM
Click here for more details...
=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================
![Salesforce Salesforce](https://1.bp.blogspot.com/-7PAEudGm-vA/X5xmpBCB6-I/AAAAAAAAACU/4l5vI_EgYqYlEXFHs1xEeyiYP9Dyl89IgCLcBGAsYHQ/s600/1.jpg)
Post a Comment