Note: The meaning of specifying only * (asterisk) as an argument in the Python function definition.

If you code it only occasionally, you'll soon forget it.

There are times when only one * (asterisk) is specified in the argument in the Python function definition (sometimes when you look at the source of the standard library):

def connect_accepted_socket(self, protocol_factory, sock, *, ssl=None):

this is,

The arguments after “” and “ identifier” are keyword-only arguments and are passed only using keyword arguments.

http://docs.python.jp/3/reference/compound_stmts.html#function-definitions

In other words, if you put *, in between, the arguments after that are "not accepted unless they are keyword arguments" (not accepted as positional arguments).

>>> def foobar(a, b, *, c=False):
...     print(a if c else b)
... 
>>> foobar(1, 2)
2
>>> foobar(1, 2, c=True)
1
>>> foobar(1, 2, True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foobar() takes 2 positional arguments but 3 were given

The word "asterisk" is for searching. Just in case you forget the fact that you made this post yourself.

Recommended Posts

Note: The meaning of specifying only * (asterisk) as an argument in the Python function definition.
Function argument type definition in python
[Python3] Call by dynamically specifying the keyword argument of the function
The eval () function that calculates a string as an expression in python
Get the caller of a function in Python
Fix the argument of the function used in map
[Note] About the role of underscore "_" in Python
About the * (asterisk) argument of python (and itertools.starmap)
Python> function> Example of taking function as an argument> map (lambda x: 2 ** x, [1, 2, 3]) / locals () ['myprint'] (3.1415, 2.718)
Set an upper limit on the number of recursive function iterations in Python
Don't take an instance of a Python exception class directly as an argument to the exception class!
Have the equation graph of the linear function drawn in Python
[Note] Import of a file in the parent directory in Python
Check the argument type annotation when executing a function in Python and make an error
Understand the function of convolution using image processing as an example
[python] Callback function (pass function as argument)
Be careful when specifying the default argument value in Python3 series
How to know the internal structure of an object in Python
Function parameters with only an asterisk'*'
Format when passing a long string as an argument of python
Try transcribing the probability mass function of the binomial distribution in Python
Calculate the editing distance as an index of pronunciation similarity [python]
A function that measures the processing time of a method in python
Utilization of lambda (when passing a function as an argument of another function)
The meaning of ".object" in Django
Get the formula in an excel file as a string in Python
[Fabric] I was addicted to using boolean as an argument, so make a note of the countermeasures.
If you want a singleton in python, think of the module as a singleton
Comparing the basic grammar of Python and Go in an easy-to-understand manner
Python Note: When you want to know the attributes of an object
[Python] Calculate the number of digits required when filling in 0s [Note]
Open an Excel file in Python and color the map of Japan
An example of the answer to the reference question of the study session. In python.
[Python] Note: A self-made function that finds the area of the normal distribution
Check the behavior of destructor in Python
The story of an error in PyOCR
The story of making Python an exe
OR the List in Python (zip function)
[Python3] Rewrite the code object of the function
The result of installing python in Anaconda
The basics of running NoxPlayer in Python
In search of the fastest FizzBuzz in Python
Python Note: The secret role of commas
Meaning of using DI framework in Python
Do not specify a mutable object (list type, dictionary type, etc.) as the initial value of the function argument of python.
[Python] I want to know the variables in the function when an error occurs!
You will be an engineer in 100 days --Day 29 --Python --Basics of the Python language 5
You will be an engineer in 100 days --Day 33 --Python --Basics of the Python language 8
You will be an engineer in 100 days --Day 26 --Python --Basics of the Python language 3
You will be an engineer in 100 days --Day 32 --Python --Basics of the Python language 7
You will be an engineer in 100 days --Day 28 --Python --Basics of the Python language 4
Output the number of CPU cores in Python
Example of taking Python> function> * args as arguments
[Note] Export the html of the site with python.
Match the distribution of each group in Python
View the result of geometry processing in Python
Specifying the range of ruby and python arrays
Install the python package in an offline environment
A note about the python version of python virtualenv
Find the divisor of the value entered in python
This is the only basic review of Python ~ 1 ~