Python becoming less Lisp-like
Steven Bethard
steven.bethard at gmail.com
Tue Mar 15 21:06:58 EST 2005
Kent Johnson wrote:
> Steven Bethard wrote:
>
>> Bruno Desthuilliers wrote:
>>
>>>> class CommandMenuSelectionCallback:
>>>> def __init__(self, key):
>>>> self.key = key
>>>>
>>>> def __call__(self):
>>>> print self.key
>>>
>>>
>>> Looks like Java.
>>
>> When was the last time you used Java? It has no support for using
>> classes as callable objects. __call__ would have to be invoked
>> manually; you definitely couldn't have a CommandMenuSelectionCallback
>> instance masquerading as a function as this code (basically) does.
>
> It's like Java in that it uses a short helper class to define an event
> callback. In the case of Java it would be an anonymous inner class
> programming to an interface. It's like Java in that it uses five lines
> of code to do what Python can do in one.
Well the second is definitely true. ;)
> Do you really prefer this verbosity to a lambda expression?
Depends on the situtation. I do like that the signature of
CommandMenuSelectionCallback is correct (that is, no arguments), where
lambda cmd=cmdkey: CommandMenuSelection(cmd)
has an optional argument when one is not asked for.
OTOH, in this particular case, the real problem is that the API has made
a poor choice for callbacks. The signature of
UI.CmdBtn.menu.add_command should be
add_command(label, command, *args, **kwargs)
instead of
add_command(label, command)
Of course API's won't always do things the way you want them to, so I
can see that in working around API deficiences, lambda can occasionally
seem useful.
STeVe
More information about the Python-list
mailing list