[Tutor] Python question 4.8.2. Keyword Argument
dn
PythonList at DancesWithMice.info
Tue Mar 5 18:07:42 EST 2024
On 6/03/24 06:52, Desiree C .Thomas wrote:
> Hello there,
>
> I hope this reaches you well.
>
> So I am following this https://docs.python.org/3/tutorial/controlflow.html
>
> I'm on 4.8.2. Keyword Argument
> Following the example
>
> def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
> print("-- This parrot wouldn't", action, end=' ')
> print("if you put", voltage, "volts through it.")
> print("-- Lovely plumage, the", type)
> print("-- It's", state, "!")
>
> So this is positional arguments
>
> parrot(1000) # 1 positional argument
> parrot(voltage=1000) # 1 keyword argument
> parrot(voltage=1000000, action='VOOOOOM') # 2 keyword arguments
> parrot(action='VOOOOOM', voltage=1000000) # 2 keyword arguments
> parrot('a million', 'bereft of life', 'jump') # 3 positional arguments
> parrot('a thousand', state='pushing up the daisies')
>
>
> 1. So I understand the parrot(1000) would just input into the required argument voltage
> 2. Parrot voltage=1000 also input into the required argument voltage
> 3.
> parrot(voltage=1000000, action='VOOOOOM') I can enter the requirement argument and skip one of the arguments to input into action. The other non stated arguments just use the default values.
> 4.
> parrot(action='VOOOOOM', voltage=1000000) Here I accepted that I can state on the arguments I want to filled in just as long as the requirement argument is inputted. I also accepted that the requirement argument doesn't need to be declare first either.
> 5.
> parrot('a million', 'bereft of life', 'jump') The requirement argument was filled and the remaining two arguments were inputted in the respective order of the functions
> 6.
> parrot('a thousand', state='pushing up the daisies') I have accepted that the requirement argument was assumed to the first argument and accept to be 'a thousand' and the declared argument was input
>
> Please feel free of my understanding so far.
> So continuing
>
>
> parrot()
>
> The requirement argument isn't inputted therefore error.
>
>
> parrot(voltage=5.0, 'dead')
>
> This is the error I get: SyntaxError: positional argument follows keyword argument on line 10
>
> From my understanding, I thought the voltage should accept voltage to be 5.0 and update state into 'dead'? Can I get an explaination for this error.
You've spotted that there are different ways of looking at function's
parameters:
- positional
- keyword, and
- default value
A positional-parameter must be matched with an argument at every
function-call. The choice of description is that the position of each
argument must match that of the function's parameter.
Contrarily keyword-parameters have no requirement for relative
positioning, and as you've observed may be supplied in any order.
A default-parameter can be considered to be optional- if it is not
supplied with an argument-value, then the default is substituted. It's
behavior is otherwise that of a keyword-parameter.
Now for the slightly-confusing part:
- just because a parameter is "positional", doesn't stop us from
labeling an argument as if it were a keyword-parameter.
- just because a parameter is "keyword", doesn't stop us from listing
the arguments without labels and as if they are positional.
Oh great!
ie don't look for hard-and-fast rules, because Python is *flexible*!
Then the issue is what happens when there is a mix of positional- and
keyword-arguments in the same function-call?
The rule is that we can start with positional-arguments, but once a
keyword-argument has been used, every argument thereafter must be a
keyword-argument.
So, positional-arguments first, and keyword-arguments last!
The positional-arguments must be in-order. The keyword-arguments may be
in any sequence.
Question (which may have occurred to you):
what happens if we call
parrot( 10_000, "dead", "voom", state="just resting" )
- which argument is applied to the state parameter?
(for you to consider and then dis/prove...)
To round-off (the difference between keyword-parameters and
default-values): arguments are required for positional-parameters (as
noted), whereas parameters with default-values may be omitted (optional).
Thus, (rare case) if use a keyword-parameter which does NOT have a
default-value, an argument must be provided (whether in positional or
keyword style).
(I've ignored the / and * symbols for defining parameter-types that follow)
--
Regards,
=dn
More information about the Tutor
mailing list