python underscore

0407 edit

Introduction

I'm sorry for the post for beginners, but I believe that there are beginners who are similarly troubled.

Underscore that comes out a little

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)

_ To ignore the return value

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.

About function name _

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

_function (x): one before the function

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?

function (x) _: one after the function

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.

__function (x): Two before the class name

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.

\ _ \ _ Function \ _ \ _ (x): Two before and after the function

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

Make the numbers easier to read _

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.

Indicates the last value displayed in the interpreter _

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.

Then what is if \ _ \ _ name__ =='\ _ \ _ main__'?

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.

in conclusion

I want to write beautiful code.

Other references

[Python cheat sheet] Special method How to put underscore in Markdown notation

Recommended Posts

python underscore
Python underscore variable
Python
kafka python
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
install python
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
Installing Python 3.4.3.
Python memo
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals
Python summary
Start python
[Python] Sort
Note: Python
Python basics ③
python log
Python basics
[Scraping] Python scraping
Python update (2.6-> 2.7)
python memo
Python memorandum
Python # sort
ufo-> python
Python nslookup
python learning
Hannari Python 2020
[Rpmbuild] Python 3.7.3.
Prorate Python (1)
python memorandum
Download python
python memorandum
Python memo
started python