What is the python underscore (_) for?


  1. Ignore the Return value

And by mastering these, you will be able to show your friends the feeling of "** I know python awesome **"!


1. Ignore the Return value

This is familiar and you often see it. abridgement.

2. Distinguish how to use by naming functions

I didn't understand this because various patterns came out.

a. _function (x): #One before function

def _single_leading_underscore(x):
    return something

A function can be defined as "internal" by adding one underscore before the function. That is, this function will not be loaded when imported from another python file. It's a little calculated, but it's complicated, so it's used with the motivation of making it a function.

b. function_ (x): #One after the function

def single_trailing_underscore_: 
    return something

I haven't seen this crazy, but adding one underscore after the function is used to avoid naming it as an important function in Python. It's probably used by troubled chans who want to use the same name as list as a function name.

But then I think I should give it a different name.

c. __function (x): #two before the function

def __double_leading_underscore: 
    return something

Call the name's mangling mechanism by underscore two before the function in class. This ridiculous "mangling mechanism" is that the interpreter and compiler stop handling variables in the usual way, for example, for the class FooBar function __boo, Foobar.__bar cannot call the function, and it is used as _Foobar__boo.

Yeah, I don't use Anma.

d. __ function __ (x): #Two before and after the function

def __double_leading_and_trailing_underscore__: 
    return something

By underscore two before and after the function in class, it becomes a magic method. This inaudible "magic method" is a method called a special method of a class in Japanese books, and it is taken care of when creating a so-called class.


That is. There are many special methods, so please check this link, but the special methods are basically It's wise to use an existing one and not define it yourself.

3. Easy to read numbers

>>> 1000000
Out: 1000000
>>> 1_000_000
Out: 1000000

Implemented in python3.6 and later, used to make long numbers easier to understand.

Rather, if you don't know this rule, you must be "Why is there an underscore in the numbers?" Is python, which is famous as a language for beginners, less readable for beginners?

4. Representative of the last displayed value in the interpreter

In the interpreter, the underscore seems to represent the last expression value. Well I do not know.

References Medium

magic method

Recommended Posts

What is the python underscore (_) for?
What is the interface for ...
What is Python? What is it used for?
[Python] What is @? (About the decorator)
[python] What is the sorted key?
Python for statement ~ What is iterable ~
What is python
What is Python
[Python] What is Pipeline ...
What is Linux for?
[Python] What is virtualenv
What is wheezy in the Docker Python image?
Wagtail is the best CMS for Python! (Perhaps)
[Example of Python improvement] What is the recommended learning site for Python beginners?
What is the activation function?
What is the Linux kernel?
[Python] Python and security-① What is Python?
[Python] * args ** What is kwrgs?
What is the Callback function?
What is a python map?
Python Basic Course (1 What is Python)
What I got into Python for the first time
Electron is the best solution for Python multi-platform development
[Python] What is a zip function?
[Python] What is a with statement?
python underscore
What is scraping? [Summary for beginners]
What is the X Window System?
See python for the first time
What is the default TLS version of the python requests module?
Python> What is an extended slice?
What is xg boost (1) (for beginners)
Command for the current directory Python
Check what the character code is for all files under the directory that is Python and output
[Introduction to Python] What is the most powerful programming language now?
What to do when the value type is ambiguous in Python?
[Python] What is pandas Series and DataFrame?
[Python] What is inherited by multiple inheritance?
What is NaN? NaN Zoya (Python) (394 days late)
Inject is recommended for DDD in Python
What kind of programming language is Python?
Python learning basics ~ What is type conversion? ~
What is the ETL processing framework clivoa?
Why Python is chosen for machine learning
[Unix] What is the zombie process / orphan process?
What is the cause of the following error?
[Statistics for programmers] What is an event?
What is a dog? Python installation volume
MongoDB for the first time in Python
[python] [meta] Is the type of python a type?
Pandas of the beginner, by the beginner, for the beginner [Python]
[Machine learning] What is the LP norm?
2016-10-30 else for Python3> for:
Python underscore variable
python [for myself]
What is copy.copy ()
What is the XX file at the root of a popular Python project?
Python is easy
What is Django? .. ..
[Introduction to Python] What is the difference between a list and a tuple?
What is dotenv?