[Tutor] Python question 4.8.2. Keyword Argument

ThreeBlindQuarks threesomequarks at proton.me
Wed Mar 6 11:11:44 EST 2024


Desiree,

Others have responded to your question about the way python handles mixtures of arguments to a function so what I am adding is a bit different.

As you note, when you write your function that may be used by others, they may not know or care about subtle rules that positional arguments come first and are mandatory.

So, in some cases, I find a style useful that makes more or even all arguments to be keyword arguments with a default.

Your first argument of voltage could be written as voltage=NULL or anything else that is meaningful to you as a way of recognizing the user did not specify a value. 

This means no error messages are supplied if the argument is not used so it is up to you, the programmer, to start your function body with appropriate code that detects what is missing and acts appropriately. Sometimes it means supplying an error message and/or returning with some error status. Other times, you can do anything from asking them to interactively supply a value (often not doable) to making up one with some calculation perhaps based on other arguments supplied.

The positive is that a function built with no positional arguments can be called very flexibly with arguments in the right order if not specified by name but also in any order if labeled.

Note not all languages come up with the same rules for something like this and python itself has in some ways evolved to currently be set up with the rules you are learning.

They may even make changes in the future.

- Q



Sent with Proton Mail secure email.

On Tuesday, March 5th, 2024 at 12:52 PM, Desiree C .Thomas <desireect at live.com> 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.
> 
> Thanks
> Desiree Cortez-Thomas
> 
> 
> 
> 
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list