What is the purpose of a bare asterisk in function arguments? What is the purpose of a bare asterisk in function arguments? python-3.x python-3.x

What is the purpose of a bare asterisk in function arguments?


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] || 3end

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.