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.
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:
def parser_f(filepath_or_buffer,
sep=sep,
dialect=None,
compression=None,
doublequote=True,
escapechar=None,
quotechar='"',
quoting=csv.QUOTE_MINIMAL,
skipinitialspace=False,
lineterminator=None,
header='infer',
index_col=None,
names=None,
prefix=None,
skiprows=None,
skipfooter=None,
skip_footer=0,
na_values=None,
na_fvalues=None,
true_values=None,
false_values=None,
delimiter=None,
converters=None,
dtype=None,
usecols=None,
engine='c',
delim_whitespace=False,
as_recarray=False,
na_filter=True,
compact_ints=False,
use_unsigned=False,
low_memory=_c_parser_defaults['low_memory'],
buffer_lines=None,
warn_bad_lines=True,
error_bad_lines=True,
keep_default_na=True,
thousands=None,
comment=None,
decimal=b'.',
parse_dates=False,
keep_date_col=False,
dayfirst=False,
date_parser=None,
memory_map=False,
nrows=None,
iterator=False,
chunksize=None,
verbose=False,
encoding=None,
squeeze=False,
mangle_dupe_cols=True,
tupleize_cols=False,
infer_datetime_format=False):
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.
For those who are coming from or/and used ruby
Below expression in python
def f(a, b, *, c=1, d=2, e=3):
is similar to
def f(a,b, options={})
c = options[:c] || 1
d = options[:d] || 2
e = options[:e] || 3
end
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.