Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an asterisk argument in an __init__ method mean? [duplicate]

What does a bare asterisk in the arguments of a function do?

When I looked at the pickle module, I see this:

pickle.dump(obj, file, protocol=None, *, fix_imports=True)

I know about a single and double asterisks preceding arguments (for variable number of arguments), but this precedes nothing. And I'm pretty sure this has nothing to do with pickle. That's probably just an example of this happening. I only learned its name when I sent this to the interpreter:

>>> def func(*):
...     pass
...
  File "<stdin>", line 1
SyntaxError: named arguments must follow bare *

If it matters, I'm on python 3.3.0.

like image 751
Eric Avatar asked Nov 17 '22 12:11

Eric


1 Answers

Bare * is used to force the caller to use named arguments - so you cannot define a function with * as an argument when you have no following keyword arguments.

See this answer or Python 3 documentation for more details.

like image 63
Kimvais Avatar answered Jan 19 '23 00:01

Kimvais