[Note] About the role of underscore "_" in Python

The role of underscore "_"

I'm reading Python sample code

def __init__():

Or

def _variable_hogehoge(x):

I often see it written with two or one underscore like.

Functions defined by two underscores are ** those that do not receive external references **. In this case, enclose it in an underscore. A function defined by one underscore can be referenced **, but basically it seems to be a convention that it is not referenced from the outside **.

I knew so far, but a sentence of this sample code

_, loss_value = sess.run([train_op,loss])

I wondered what the underscore was "[Practice of assigning tapple values not used in Python to“ _ ”(underscore)](http://momijiame.tumblr.com/post/28130976849/python-%E3%81%A7%E4%BD% BF% E3% 82% 8F% E3% 81% AA% E3% 81% 84% E3% 82% BF% E3% 83% 97% E3% 83% AB% E3% 81% AE% E5% 80% A4% E3% 81% AF-% E3% 82% A2% E3% 83% B3% E3% 83% 80% E3% 83% BC% E3% 83% 90% E3% 83% BC-% E3% 81% AB% E4% BB% A3% E5% 85% A5% E3% 81% 99% E3% 82% 8B% E3% 81% A8% E3% 81% 84% E3% 81% 86% E3% 83% 97% E3% 83% A9% E3% 82% AF% E3% 83% 86% E3% 82% A3% E3% 82% B9) "

Well, it's nonsense to assign a variable to an unused return value, so let's ignore it with an underscore.

I feel that it will be beautiful programmatically.

Recommended Posts

[Note] About the role of underscore "_" in Python
Python Note: The secret role of commas
A note about the python version of python virtualenv
About the behavior of Model.get_or_create () of peewee in Python
About the ease of Python
About the features of Python
A reminder about the implementation of recommendations in Python
About the basics list of Python basics
[Note] Import of a file in the parent directory in Python
I used Python to find out about the role choices of the 51 "Yachts" in the world.
Check the behavior of destructor in Python
About the virtual environment of python version 3.7
The result of installing python in Anaconda
The basics of running NoxPlayer in Python
In search of the fastest FizzBuzz in Python
Output the number of CPU cores in Python
[Python] Sort the list of pathlib.Path in natural sort
[Note] Export the html of the site with python.
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
the zen of Python
Make a copy of the list in Python
[Python] Calculate the number of digits required when filling in 0s [Note]
About the difference between "==" and "is" in python
Find the divisor of the value entered in python
Data analysis in Python: A note about line_profiler
Find the solution of the nth-order equation in python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
About __all__ in python
About the * (asterisk) argument of python (and itertools.starmap)
Experience the good calculation efficiency of vectorization in Python
Sort in Python. Next, let's think about the algorithm.
How to get the number of digits in Python
Note the frequently used options in Python + Selenium + Chrome
About the inefficiency of data transfer in luigi on-memory
Note when putting lxml of python package in ubuntu 14.04
Learn the design pattern "Chain of Responsibility" in Python
Implement the solution of Riccati algebraic equations in Python
Reproduce the execution example of Chapter 4 of Hajipata in Python
Let's use the open data of "Mamebus" in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
[Python] Outputs all combinations of elements in the list
Get the URL of the HTTP redirect destination in Python
Reproduce the execution example of Chapter 5 of Hajipata in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Check the asymptotic nature of the probability distribution in Python
Python Note: The mystery of assigning a variable to a variable
Towards the retirement of Python2
Download the file in Python
Find the difference in Python
About the Python module venv
Think about architecture in python
About the enumerate function (python)
Python Input Note in AtCoder
About various encodings of Python 3
Equivalence of objects in Python
About the components of Luigi
Implementation of quicksort in Python
About "for _ in range ():" in python