What is the "Unpacking Arguments List" rule?

dieter dieter at handshake.de
Wed Jun 13 00:59:07 EDT 2018


Jach Fong <jfong at ms4.hinet.net> writes:
> ...
> 4.7.4. Unpacking Argument Lists
> The reverse situation occurs when the arguments are already in a list or
> tuple but need to be unpacked for a function call requiring separate
> positional arguments.
>     ...
>>>> args = [3, 6]
>>>> list(range(*args))
> """
>
> I can't understand why line 19 works?

Not sure what "line 19" is - but if it refers to the example above:

  "range" accepts (among others) 2 integer arguments.
  The "*args" above means: unpack the sequence in "args" into
  individual arguments.
  This means (with the values of the example above),
  that "range(*args)" is equivalent to "range(3, 6)".

> Didn't it violate the rule of
> "# non-keyword argument after a keyword argument"?

No keyword arguments at all in the above example.

> and why a more
> reasonable look syntax gets an error?
>
>     action(*args, progress)
>                      ^
> SyntaxError: only named arguments may follow *expression  File
> "test.py", line 19

This is (in my view) a somewhat arbitrary restriction -- maybe
introduced for symmetry with the function definition syntax.

The message is quite clear, however: after the "*arg",
you must pass keyword arguments only, i.e. they must have
the form "param=value".

> The only reason I can guess is that it checks with the rule in 4.7.3
> which is really unrelated. The "*any" notation used in different places
> with different meaning, such as defining arbitrary argument, unpacking
> argument or even in an assignment(a,*b=any). Maybe it will be better to
> stop this syntax checking and lets both statements below valid:-)

The "*args" and "**kw" are very helpfull. I hope (and expect)
they will remain.




More information about the Python-list mailing list