0407 edit
I'm sorry for the post for beginners, but I believe that there are beginners who are similarly troubled.
Would you like to read a person's code or reference book and come up with it? ** This underscore isn't it! Underscore like _ **
python
H,W,_ = img.shape
Or first
python
__main__
Why is there an underscore? Like. This time is such a story.
This time as well, I found a god article while I was worried about it, so I will flesh it out and chew it based on it. Click here for the article about God who solved my worries
[Let's master the underscore (_) of "Pythonic various" Python!](Https://medium.com/lsc-psd/pythonic%E8%89%B2%E3%80%85-python%E3%81% AE% E3% 82% A2% E3% 83% B3% E3% 83% 80% E3% 83% BC% E3% 82% B9% E3% 82% B3% E3% 82% A2-% E3% 82% 92 % E4% BD% BF% E3% 81% 84% E3% 81% 93% E3% 81% AA% E3% 81% 9D% E3% 81% 86-3c132842eeef)
python
H,W,_ = img.shape
This is exactly what I gave in the first example. This is it. .. .. This ignores the third return value of .shape-underscore Quoted because the citation source explained the excess and deficiency Says
Python has many libraries, and I often import and use functions. In such a case, if there are multiple return values from the function and there is a part that is not used, you can use the underscore and discard it without occupying the memory of the return value.
With this, you can build a program without using wasted memory. This seems to be the most used.
It seems that there are four types depending on the location. Moreover, it is defined in the PEP8 coding standard. I didn't even know that there was such a definition. https://www.python.org/dev/peps/pep-0008/
It is described in detail in PEP8> Naming Convention. Then I will summarize the following four
python
def _single_leading_underscore(x):
return n
#weak "internal use" indicator. E.g. from M import *
#does not import objects whose names start with an underscore.
If you write this, it will not be called by func import * from another place, but it seems that you can call a function with an underscore as a function in class such as classx._func (). Basically, it seems to be the practice of python to not accept external references, but to add one underscore to it. Is this also a read measure?
python
def list_(x):
return n
#used by convention to avoid conflicts with Python keyword
If you want to use python reserved words such as list and class for function names, it seems that you should add one _ at the end.
python
def __double_leading_underscore:
return n
#when naming a class attribute, invokes name mangling
#(inside class FooBar, __boo becomes _FooBar__boo; see below).
Preceding a function in a class with two underscores will call the named ** mangling **. Mangling means name mangling, and in short, it seems that it is not treated as ordinary internally. As written in the above example, the variable \ _ \ _boo in class FooBar cannot be called by FooBar. \ _ \ _Boo, and can only be called by \ _FooBar \ _ \ _boo. This also does not accept access from the outside. I think it can be said to be one of the countermeasures.
python
def __double_leading_and_trailing_underscore__:
return n
# "magic" objects or attributes that live in user-controlled
# namespaces. E.g. __init__, __import__ or __file__. Never invent
# such names; only use them as documented.``
If you add two underscores before and after the function in class, it becomes Magic Method (special method). It's a story about special methods, but it seems that they are used when you want to change the behavior of an object or make it move according to the nature of the class. For example, \ _ \ _ init \ _ \ _ uses the passed arguments. It initializes the newly created object in the class definition. There are many magic methods. It's on the list, but it's full http://www.ops.dti.ne.jp/ironpython.beginner/method.html
python
>>> 1_000_000
Out: 1000000
[Reference](https://medium.com/lsc-psd/pythonic%E8%89%B2%E3%80%85-python%E3%81%AE%E3%82%A2%E3%83%B3% E3% 83% 80% E3% 83% BC% E3% 82% B9% E3% 82% B3% E3% 82% A2-% E3% 82% 92% E4% BD% BF% E3% 81% 84% E3 % 81% 93% E3% 81% AA% E3% 81% 9D% E3% 81% 86-3c132842eeef)
Starting with Python 3.6, you can add underscores in numbers to make them easier to read. You can realize a comma every 3 digits that are commonly used.
It seems that even if you type an underscore into a number instead of a comma every 3 digits, it will be recognized as a proper number. It's convenient when you want to insert a large number of digits.
python
>>> 10
Out: 10
>>> _ * 3
Out: 30
>>> _ * 10
Out: 300
I didn't know this, but when using the python interpreter, it seems that you can get the previously displayed number with an underscore.
** Addendum 0406 ** As you pointed out in the comment, if you use \ _ as a variable name, it seems to work as an interpreter to display the value put in the variable instead of the previously displayed numerical value. Please refer to the comments for details. It gives an easy-to-understand example. Thank you.
I found a site that is beautifully organized The meaning and usage of Python if \ _ \ _ name \ _ \ _ =='\ _ \ _ main \ _ \ _'
It seems that it means "execute the subsequent processing only when the corresponding file is executed as a script from the command line". In other words, it doesn't work when called from somewhere
When I read the code of others, I feel that it is written to show that this is the main one.
I want to write beautiful code.
[Python cheat sheet] Special method How to put underscore in Markdown notation
Recommended Posts