Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How does underscore \"_\" work in Python files?
In Python, the underscore (_) is a special character with different meanings based on how and where it is used. It plays a significant role in readability, naming conventions, and interpreter behavior. In this article, we will discuss different scenarios of using the Underscore(_) in Python.
Single Underscore _ as Throwaway Variable
The single underscore (_) is commonly used as a throwaway variable when you don't need to use a particular value in loops or unpacking operations.
Ignore Values in Loops
When we don't care about the loop variable value, we use _ to indicate this ?
for _ in range(5):
print("Processing...")
Processing... Processing... Processing... Processing... Processing...
Ignoring Values in Unpacking
Use _ to ignore unwanted values when unpacking sequences ?
name, _, age = ("Alice", "middle_name", 25)
print(f"Name: {name}, Age: {age}")
Name: Alice, Age: 25
Single Underscore _ in Python REPL
In the Python Interactive Shell (REPL), the single underscore _ stores the result of the last evaluated expression ?
>>> 15 + 25 40 >>> _ * 2 80 >>> _ - 10 70
Single Leading Underscore (_variable)
A single leading underscore indicates that a variable or method is intended for internal use. This is a naming convention, not enforced by Python, but signals "private" usage ?
class BankAccount:
def __init__(self, name, balance):
self.name = name # Public attribute
self._balance = balance # Internal use convention
def deposit(self, amount):
self._balance += amount
def get_balance(self):
return self._balance
account = BankAccount("John", 1000)
print(account.name) # Public access
print(account.get_balance()) # Proper way to access balance
print(account._balance) # Possible but not recommended
John 1000 1000
Single Trailing Underscore (variable_)
A single trailing underscore avoids naming conflicts with Python's reserved keywords or built-in names ?
class_ = "Mathematics" # Avoid conflict with 'class' keyword
type_ = "String" # Avoid conflict with built-in 'type'
list_ = [1, 2, 3, 4] # Avoid conflict with built-in 'list'
print(f"Subject: {class_}")
print(f"Data type: {type_}")
print(f"Numbers: {list_}")
Subject: Mathematics Data type: String Numbers: [1, 2, 3, 4]
Translation Function Placeholder
In internationalized applications, _ is commonly used as an alias for translation functions ?
# Simulating a translation function
def translate(text):
translations = {
"Hello": "Hola",
"Goodbye": "Adiós",
"Thank you": "Gracias"
}
return translations.get(text, text)
# Using _ as alias for translation
_ = translate
print(_("Hello"))
print(_("Thank you"))
print(_("Welcome")) # No translation available
Hola Gracias Welcome
Comparison of Underscore Usage
| Pattern | Purpose | Example |
|---|---|---|
_ |
Throwaway variable | for _ in range(3) |
_variable |
Internal use convention | self._balance |
variable_ |
Avoid keyword conflicts | class_ = "Math" |
_ (REPL) |
Last expression result |
>>> 5+3 then >>> _
|
Conclusion
The underscore (_) in Python serves multiple purposes: as a throwaway variable, internal use indicator, keyword conflict resolver, and REPL result holder. Understanding these conventions helps write more Pythonic and readable code.
