[Tutor] Why include "*args" in a function's parameter list when no args are ever passed in?

Peter Otten __peter__ at web.de
Thu Sep 3 04:13:13 EDT 2020


boB Stepp wrote:

> I am reading an interesting Tcl/Tk tutorial at tkdocs.com/tutorial .  In
> it the author shows how each GUI example would be implemented in several
> languages (Tcl, Ruby, Perl, Python).
> 
> So far I have noticed a commonality to all of his Python code involving
> callback functions.  Following is a typical example from his "Listbox"
> section of https://tkdocs.com/tutorial/morewidgets.html :
> 
> # 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.

Personally I would still prefer the signature

def showPopulation(_event=None):
    ...




More information about the Tutor mailing list