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