What is the "Unpacking Arguments List" rule?
sales at caprilion.com.tw
sales at caprilion.com.tw
Wed Jun 13 02:21:53 EDT 2018
[Of the first part]
line 19 is
action(progress=progress, *args)
where the args is a tuple
args = (i, 3)
and the function is defined as
def action(id, reps, progress):
In documents 4.7.2. Keyword Arguments, it says
'''
def parrot(voltage, state='a stiff', action='voom', type='Norwegian
Blue'):
...
...
but all the following calls would be invalid:
...
parrot(voltage=5.0, 'dead') # non-keyword argument after a keyword
argument
...
'''
After unpack the args in line 19, it will be looks like
action(progress=progress, i, 3)
and it seems violate the above rule.
[Of the second part]
> The "*args" and "**kw" are very helpfull. I hope (and expect)
> they will remain.
There is no "**kw" involved in this topic:-) and the positional limit of
"*args" in a function definitions has its own good reason:-)
IMHO, there is no reason to check the *args has to appear at last in
positional argument list in a function call because of there is no
"unknown number of parameters" at the time of unpacking. It should be
alright to write line 19
action(*args, progress)
just like assignment below where both are valid.
a, *b = any
*a, b = any
Best Regards,
Jach Fong
dieter at 2018/6/13 PM 12:59 wrote:
> 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.
>
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
More information about the Python-list
mailing list