Parameterized functions of no arguments?

Rotwang sg552 at hotmail.co.uk
Fri Feb 11 01:25:14 EST 2011


On 11/02/2011 05:42, Ben Finney wrote:
> Rotwang<sg552 at hotmail.co.uk>  writes:
>
>> Here's something that does work:
>>
>>      menu = Tkinter.Menu(master, tearoff = 0)
>>      for k in x:
>>          def f(j = k):
>>              [do something that depends on j]
>>          menu.add_command(label = str(k), command = f)
>>
>> Still, I'd like to know if there's a more elegant method for creating
>> a set of functions indexed by an arbitrary list.
>
> That already seems quite elegant to me.

Thanks.


> What part is inelegant to your eye?

I guess the fact that it exploits the way Python evaluates default 
function arguments to achieve something other than what they were 
intended for. Still, I see that Chris suggested the same solution 
(thanks, Chris) so it clearly isn't something that's frowned upon.



More information about the Python-list mailing list