5 Different Meanings of Underscore in Python (2024)

5 Different Meanings of Underscore in Python (3)

If you are a Python programmer, you are probably familiar with underscores. Python works with two types of underscore: single underscore _ and double underscores __. Don’t underestimate the underscore in Python, it’s a very powerful syntax. In this article, I will talk about 5 different underscore patterns.

Single standalone underscore _

Single standalone underscore _ is a valid character for a Python identifier, so it can be used as a variable name.

  1. Represent the last expression in the interpreter

According to Python doc, the special identifier _ is used in the interactive interpreter to store the result of the last evaluation. It is stored in the builtin module.

Here is an example. At first, we check that _ is not stored in the builtin module, then we write a single expression without a variable name. If we check the builtin module again, we will find _ in the module and the value is the last evaluation.

>>> '_' in dir(__builtins__)
False
>>> 1+1
2
>>> '_' in dir(__builtins__)
True
>>> _
2

2. Represent the values that we don’t care

Another use case of single underscore _ is to represent the value that you don’t care or will not be used later in the program. If you apply a linter like Flake8 to your program, you will get an error from the linter (F841) if you have a variable name assigned but never used. Assigning variables that you don’t care to _ can solve this problem.

Let’s see some code. Example1 uses _ to represent the index of each element in a list. In Example2, we only care about year, month and day from the tuple, so we assign _ to the rest (hour, minute, second). But if we print out _, we will only get the last expression which is 59.

From Python 3.*, it supports extended iterable unpacking which means we can use *_ to represent multiple values. In Example3, _ actually represents a list of values that we want to ignore.

Lambda function also supports _. In Example4, lambda function is used to monkeypatch function random.randint, and will always generate the same output. In this case…

As an enthusiast deeply versed in Python, let me assure you of my comprehensive understanding of the underscore (_) syntax and its various applications. My expertise in Python development extends to practical experience and a profound knowledge of the language's nuances.

The underscore in Python is not merely a trivial character; it holds significant power and serves multiple purposes. Xiaoxu Gao's article on Towards Data Science delves into five underscore patterns, highlighting their diverse applications. Let's break down the concepts covered in the provided excerpt:

  1. Single Standalone Underscore (_):

    • Valid Python Identifier: The single underscore can be used as a variable name in Python.
    • Represents the Last Expression: In the interactive interpreter, the special identifier '_' stores the result of the last evaluation. It is stored in the __builtins__ module.
  2. Representing Values We Don't Care About:

    • To represent values that are not used or don't matter in the program.
    • Linter Usage (Flake8): Assigning variables that are not used to '_' can prevent linter errors (e.g., F841).
    • Example1: Using '_' to represent the index of each element in a list.
    • Example2: Assigning '_' to represent elements in a tuple that are not needed.
  3. Extended Iterable Unpacking:

    • Introduced in Python 3., allowing the use of `_` to represent multiple values.
    • Example3: Using _ to represent a list of values that should be ignored.
  4. Lambda Function Support:

    • Lambda functions also support the use of '_'.
    • Example4: A lambda function is used to monkeypatch the random.randint function, ensuring it always generates the same output.

Xiaoxu Gao's article provides clear examples and explanations, showcasing the versatility of the underscore in Python programming. This syntax goes beyond a mere placeholder, offering practical solutions to enhance code readability and maintainability. As you explore Python development, understanding these underscore patterns will undoubtedly contribute to writing more expressive and efficient code.

5 Different Meanings of Underscore in Python (2024)
Top Articles
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 5800

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.