Why Am I Seeing _ Everywhere in Python Code?

Kieron Spearing
Dev Genius
Published in
6 min readJan 21, 2022

--

Seriously?! I cannot be the only one that had this question in the beginning!

An underscore against mobile phine with laptop and keyboard in the background laying over a tablecloth
Image by Fernando Hernandez from unsplash

After some time in Python development and working in a company where there were many people to learn from I noticed the underscore being used in many ways other than that which I knew.

Naturally, this made me both curious and a little annoyed as to why I didn’t understand the full potential of a simple character in the Python Programming Language.

Although this is a rather simple subject in the language, being the type of person I am, I could not be satisfied completely until I had gone through the many uses, done more research into them, and even played with each of them myself.

Thus began yet another winding road of research, investigation, and fun as I pursued the knowledge I required and became comfortable in the use of this in my code daily.

In this post, I will be going over the different uses of the underscore in Python code that came to light in my research, some well known, while others not so much.

When we take a closer look as to how the underscore is used we can typically separate it into three separate areas: Naming, The Interpreter, and Useful Development Tricks.

Each of these can be quite useful to use in your code daily and help one write clean code more efficiently.

Naming

There are multiple ways in which the underscore is used in Python, but when looking at how it is used in naming there are five main uses to mention.

  1. Snake Case Naming Convention.
  2. Single Leading Underscore.
  3. Single Post Underscore.
  4. Double Leading Underscore.
  5. Double Leading and Post Underscore.

Snake Case Naming Convention

When we refer to the snake case naming convention we are referring to the way we use the underscore to separate words in the name, this is the recommended naming convention for function and variable names according to PEP-8 to write clean, beautiful Python code.

Single Leading Underscore

When we use a leading underscore before variable, function, or method names it is to indicate to the developer that it is only for internal use within the class.

The intention of this is for it to be treated as non-public, meaning that if importing from a module using the asterisk *, all names beginning with a leading underscore will not be imported.

For example, if we have these functions and variables saved in a script called example.py and we try to import it and access the variables and functions, then we will not be able to access those with a leading underscore.

Example of explained usage

However considering that Python doesn’t truly support private variables and methods, if we were to call them directly from the module as below, then we can access these variables and functions.

We can also identify above that even members of a class are not truly private, thus this is mainly used to indicate the intention to the developers working with said code.

Single Post Underscore

This particular type of usage of the underscore is used to help avoid naming conflicts and is particularly useful if you want to use any of the Python keywords as names for variables, classes, or functions.

While this is quite useful, it is not recommended simply because it is good practice to use clear concise names within the application and if you want to indicate the type required there are better methods to do so, such as the docstring or type hinting.

Double Leading Underscore

The double leading underscores are used for name mangling in Python.

What this essentially means in Python is that we use this for class attributes that we do not want subclasses to use and this is done by giving them a name with two leading underscores.

How the interpreter does this is, at runtime any identifier with a double leading underscore is replaced with _classname__identifier, where the classname is the current class name with a leading underscore.

This can be very useful for letting subclasses override methods without breaking other method calls. This can be used both for methods and for variables however, it is important to note that this only happens when it is done within the class definition.

Double Leading and Post Underscore

Whenever we find methods with both a double leading underscore and a double post underscore, these are known as magic or dunder methods.

These methods provide special features to the names however, this type of naming should be avoided completely, as it will lead to clashes if we attempt to name variables or methods using this convention.

“Sometimes it’s better to leave something alone, to pause, and that’s very true of programming.”Joyce Wheeler

The Interpreter

Another use of the underscore in Python is in the interpreter and we don’t have to do anything to work with this.

Essentially the Python Interpreter automatically stores the value of the last executed expression into a variable called _. We can also assign the value of this variable to another variable as illustrated below.

This can be quite handy to remember when wanting to work with the result of an expression within the code, without having to assign it yourself.

Useful Development Tricks

While we are talking about useful features related to the underscore, there are three other useful features of the underscore in Python, and on discovering these I found it quite handy to use in my code.

Using _ in Loops and Comprehensions

A handy use for the underscore is as the variable used within loops or comprehensions, while there are times you want the variable to have a descriptive name there are also times where you do not need to worry about it.

Separating Digits of Numbers

This is less of a trick and more of a way to simply improve the readability for the developers.

Essentially when you end up having long digit numbers, binary, octal, or hex numbers, we can use the underscore to separate it to make it more understandable for humans reading the code.

Ignoring Values

This is quite a handy usage of the underscore and is particularly useful when we are unpacking variables in Python.

What is unpacking?

When we refer to unpacking in Python, we are referring to the action of placing a tuple or list of variables on the left side of an operator, not even requiring the use of parenthesis and allowing the interpreter to automatically assign the variables according to the position in the tuple.

This action can be done with any type of iterable in Python, the only condition being that the iterable yields exactly one item per variable in the receiving tuple or list.

Now when doing this you may wish to ignore a particular variable or even all the variables except the first and the last, well this is when the underscore is immensely useful as demonstrated below.

Phew! That was a deep-dive!

If you stuck with me through the entire article thank you and I hope with this research you managed to learn something new, something you may even use in your code.

You may be wondering why it is I always end up going so deeply into subjects that may appear unimportant, but in my opinion, when you want to learn something properly you need to do the deep dives to understand what is happening underneath it all, no matter how trivial the knowledge may be.

Do you also tend to do deep dives into what may seem trivial knowledge?

--

--

I am a Full-Stack Developer at StyleSage and a Food enthusiast with 2 years experience in technology and 7 years experience working in Michelin Star restaurants