And by mastering these, you will be able to show your friends the feeling of "** I know python awesome **"!
This is familiar and you often see it. abridgement.
I didn't understand this because various patterns came out.
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.
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.
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.
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.
__init__
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.
>>> 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?
In the interpreter, the underscore seems to represent the last expression value. Well I do not know.
References Medium
Recommended Posts