Not All Underscores Are Equal in Python
By: Yong Cui
Tags: Python Programming, Back End, Programming, Python, Data Science

3. Separating Digits of Numbers

This feature is mostly for readability purposes. The following figure shows the pertinent examples. As you can see, the first pair (i.e., a0 and a1 ) of numbers are equal and so are the second pair:

4. Part of a Variable or Function Name

The most common use of underscores is to use them as part of a variable or function name. The following code snippet shows you some examples:

As you can see, for variables, the underscore can be at the beginning, in the middle, or at the end of a variable. Moreover, an underscore can be a variable name on its own.

The underscores can be used in the function names as flexibly as in the variable names. It’s valid to use them at the beginning, middle, end, or on their own. However, depending on the number of underscores and their positions, they can have different indications and use scenarios, which are summarized below.

4.1. Private attributes and methods

As mentioned previously, an underscore can be a prefix of a function. When these functions are defined in a custom class, these functions become private. As shown below, we declare a Fruit class, in which we define three attributes: healthy , _price , and __vitamines , and three methods, grow() , __sprout() , and _flower() .

As shown at the end of [10], a method without an underscore prefix is public, which can be called outside the scope of the class. One underscore signifies that the method is private, but can be called from the outside if we choose to do so in [11]. Two underscores preceding the function name make the method strictly private such that it cannot be called outside, as indicated by an error message for [12].

Similarly, the same rules apply to the attributes of a class. The pertinent examples are shown below:

Although it appears that by using double underscores preceding an attribute and method names, we are able to make them private, this is not really true. As a matter of fact, double underscores signify “name mangling,” which refers to the change of these attributes/methods using the formula: _ClassName__attributeName or _ClassName__methodName .

The examples are shown below:

One relevant scenario is that when we import from a certain module, the attributes and methods with a prefix of an underscore won’t be imported. Here’s a simplified example. Suppose that we have a module called medium_module as below.

When we use this module by calling from medium_module import * , these two functions are not both available — the one with an _ prefix isn’t available.

However, it should be noted that we can have access to these “private” methods if we import the module by calling import medium_module as mm as below:

4.2. Avoiding naming conflicts

This is a less common scenario. Python has some keywords, like def , impor t, and from , reserved for special purposes. Normally, we can’t use these keywords in our functions, which will otherwise create naming conflicts. To solve that, we can use a trailing underscore following these keywords.

The following is a simplified example of this. As you can see, the use of from_ is valid while the use of from is invalid:

4.3. Magic Methods

In the 4.1, we mentioned that naming mangling will happen if the attribute or method has double leading underscores. However, there’s a special case where name mangling won’t happen: when attributes or methods start and end with double underscores: