[Tutor] Python question 4.8.2. Keyword Argument

ThreeBlindQuarks threesomequarks at proton.me
Wed Mar 6 15:02:21 EST 2024


Desiree,

I won't repost what they wrote but will point you to the archives where all messages can be viewed various ways by you and anyone:

https://mail.python.org/pipermail/tutor/

For March of 2024 this is the thread:

[Tutor] Python question 4.8.2. Keyword Argument   Desiree C .Thomas
[Tutor] Python question 4.8.2. Keyword Argument   dn
[Tutor] Python question 4.8.2. Keyword Argument   Mats Wichmann
[Tutor] Python question 4.8.2. Keyword Argument   ThreeBlindQuarks
[Tutor] Python question 4.8.2. Keyword Argument   Desiree C .Thomas

The specific replies by Dave Neal and Mats Wichman are at:

https://mail.python.org/pipermail/tutor/2024-March/120966.html

https://mail.python.org/pipermail/tutor/2024-March/120967.html

I hope this helps.

The bottom line is the people who created this functionality had to write some general code that knows what the function declaration looked like including local names for the arguments and whether they had a default and also sees what the caller to a function supplies, and have to do a fairly complex task of matching positional and other parameters and applying some logic. Choices were made and you are learning the rules they chose.

I have seen other languages make other choices, as mentioned, such as whether they match a partial text like sta=whatever to mean state=whatever or what to do if an option is repeated.

As to why you did not see the other replies, who knows? It may have landed in your junk folder or maybe you only see it if people hit reply all? Also note their reply is embedded around and within your original text.

- Kyu 

Sent with Proton Mail secure email.

On Wednesday, March 6th, 2024 at 11:46 AM, Desiree C .Thomas <desireect at live.com> wrote:

> Hey Q,
> 
> I haven't seen the other replies to my question. Do you know where I can view them?
> 
> I have just seen yours. Also can I email you if I have further questions on Python?
> 
> Thank you
> Desiree
> ________________________________
> From: ThreeBlindQuarks threesomequarks at proton.me
> 
> Sent: 06 March 2024 16:11
> To: Desiree C .Thomas desireect at live.com
> 
> Cc: tutor at python.org tutor at python.org
> 
> Subject: Re: [Tutor] Python question 4.8.2. Keyword Argument
> 
> 
> 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
> 
> _______________________________________________
> 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