I've seen this SO question (this is not a duplicate): Python bare asterisk in function argument
In python-3.x you can add a bare *
to the function arguments, this means that (quote from docs):
Parameters after “*” or “*identifier” are keyword-only parameters and may only be passed used keyword arguments.
Ok, so, I've defined a function:
>>> def f(a, b, *, c=1, d=2, e=3):
... print('Hello, world!')
...
I can pass c
, d
and e
variable values only by specifying keywords:
>>> f(1, 2, 10, 20, 30)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() takes 2 positional arguments but 5 were given
>>> f(1, 2, c=10, d=20, e=30)
Hello, world!
Questions are:
- What is the motivation for this kind of restriction/syntax sugar?
- What use cases does it cover?
- Is it really used in third-party libraries that switched to python3?
Some "real-world" examples would help a lot. Thanks in advance.
For those who are coming from or/and used
ruby
Below expression in python
is similar to
in ruby.
Since, python is explicit is better than implicit langauge, it requires
*
(splat) operator in parameters.PS: I never used python, if i am mistaken please correct me.
PEP 3102 explains the rationale pretty clearly: the point is to allow functions to accept various "options" that are essentially orthogonal in nature. Specifying these positionally is awkward both on the defining and calling side, since they don't have any obvious "priority" that would translate into a positional order.
There are lots of example of functions that would benefit from this in various libraries. For instance, the call signature of
pandas.read_csv
is:Except for the filepath, most of these are orthogonal options that specify different aspects of how a CSV file is to be parsed. There's no particular reason why they would be passed in any particular order. You'd go nuts keeping track of any positional order for these. It makes more sense to pass them as keywords.
Now, you can see that pandas doesn't actually define them as keyword-only arguments, presumably to maintain compatibility with Python 2. I would imagine that many libraries have refrained from using the syntax for the same reason. I don't know offhand which libraries (if any) have started using it.