[Tutor] Why include "*args" in a function's parameter list when no args are ever passed in?
boB Stepp
robertvstepp at gmail.com
Thu Sep 3 20:41:23 EDT 2020
On Thu, Sep 03, 2020 at 10:13:13AM +0200, Peter Otten wrote:
>boB Stepp wrote:
>
>> # Called when the selection in the listbox changes; figure out
>> # which country is currently selected, and then lookup its country
>> # code, and from that, its population. Update the status message
>> # with the new population. As well, clear the message about the
>> # gift being sent, so it doesn't stick around after we start doing
>> # other things.
>> def showPopulation(*args):
> print(args)
>> idxs = lbox.curselection()
>> if len(idxs)==1:
>> idx = int(idxs[0])
>> code = countrycodes[idx]
>> name = countrynames[idx]
>> popn = populations[code]
>> statusmsg.set("The population of %s (%s) is %d" % (name, code,
>> popn))
>> sentmsg.set('')
>>
>> I don't understand why he just doesn't leave the parameters area in the
>> function definition blank instead of inserting "*args" which never gets
>> used. Why would one do this?
>
>Add the print statement and you'll see that while the initial explicit call
>is without arguments subsequent calls triggered by changing the listbox
>selection pass an Event object.
Ah! I forgot about this.
>Personally I would still prefer the signature
>
>def showPopulation(_event=None):
> ...
If I had seen "_event=None" instead of "*args" I would have immediately
recalled what I had otherwise forgotten and never have asked my question!
Thanks, Peter!
--
Wishing you only the best,
boB Stepp
More information about the Tutor
mailing list