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

Alan Gauld alan.gauld at yahoo.co.uk
Thu Sep 3 04:15:55 EDT 2020


On 03/09/2020 04:18, boB Stepp wrote:

> def showPopulation(*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?

I can't really speak for the author but one possibility is that he is
enabling his callback to be used in either a command=cb style or a
bind() style. In the command= style no arguments are passed to the call
back. In a bind style the event is passed to the cb. By using *args the
same callback can be used for either and it just ignores the event
argument if present.

Personally I deal with that by using a lambda wrapper around the
callback in a bind but *args would work too.

But maybe somebody else can come up with an alternative reason...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list