Underscores in Python have four special usage scenarios. Some of them are conventions to improve readability of code. Some of them have special functions we must know. The four scenarios are:
- Separate digits of numbers
- Use to ignore some variable
- Store the last expression’s value in interpreter
- Naming conventions
This post will talk about all of them to help you totally understand how to use underscores in Python.
Separate Digits of Numbers
Sometimes, if a number is too long, it’s hard and boring to count how many digits this number has. For example, if there is a variable
a=1000000000000, how many zeros in
Fortunately, Python gives us a very simple way to use underscores as visual separators for digit grouping purposes in integral, floating-point and complex number literals. If we need to define a long number, don’t forget to use underscores to make life easier.
>>> a = 1_000_000_000_000
The above statement of
a=1_000_000_000_000 is totally the same as
a=1000000000000 . Which way do you like to define the
a ? 😄
Use to Ignore Some Variable
Sometimes, a part of values of a container(list/tuple and so on) are useless. We can use an underscore to ignore them.
>>> L = [1,3,5,7]
>>> a, _, b, _ = L
>>> print(a, b)
As the above code shown, we need to get the first and third values of
L and assign them to variables
b. In the meanwhile, the second and last values are useless. We just use
_ to ignore them.
Note: This usage scenario is just a convention. We can use any other names to do this. But using underscore is best choice to avoid ambiguity and let other developers understand it easily.
Store the Last Expression’s Value in Interpreter
This is an interesting and small trick of Python Interpreter. We can use one underscore to simply get the last expression’s value.
Underscores are often used in variable,class and function names. For example:
first_name = 'Yang'
When we define the variable
first_name , we used a
_ to make it more readable.
Besides using underscores in the middle position of a name, there are four special naming patterns we should know:
- Single Leading Underscore:
- Double Leading Underscore:
- Single Trailing Underscore:
- Double Leading and Trailing Underscore:
Single/Double Leading Underscore
In object-oriented programming, there are three types of class members:
- Public: The public members of a class are accessible from any part of the program. In Python, all member variables and methods without leading underscores are public members by default.
- Protected: The protected members are accessible in the same class or derived classes. We use a single leading underscore to define a variable as a protected member.
- Private: The private members are accessible only in the same class. We use double leading underscores to define a variable as a private member.
def __init__(self, name, age, grade):
self.name = name # public member
self._age = age # protected member
self.__grade = grade # private member
There are two tips about leading underscores:
- The single leading underscore is just a convention. We can still use it as a public member. But we should not do this. Following good programming conventions will make our code more elegant and easy to be read by other developers.
- The double leading underscores mechanism is a syntax rather than a convention. Python uses name mangling technique to ensure we won’t use a private member out of its class.
Single Trailing Underscore
Sometimes, we think of a good name for a variable, but unfortunately, the name conflicts with Python keywords or built-ins. We can just use a single trailing underscore to avoid ambiguity. For example:
>>> list_ = [1,2,3] # good naming style
>>> list = [2,3,4] # bad name; has ambiguity with Python keyword
Double Leading and Trailing Underscore
This convention is used for special variables or methods (so-called “magic method”) of a Python class, such as
__init__() method. We just need to know that this convention is used for Python magic methods and avoid naming our variables or methods like this.
A good developer should follow common code conventions and pay attention to details. Using underscores properly in Python is a good habit.
Thanks for reading. More Python tutorials: