annonymous functions -- how to
Dave Benjamin
ramen at lackingtalent.com
Fri May 6 19:57:22 EDT 2005
Peter Hansen wrote:
> Dave Benjamin wrote:
>
>> def add_thingy():
>> def func(thingy_id):
>> print 'got thingy id:', thingy_id
>> def funnc(doodad_id):
>> print 'got doodad id:', doodad_id
>> def func(thingy_doodad):
>> print 'thingy doodad created, froobling...'
>> frooble(thingy_doodad)
>> print 'froobling complete'
>> with_new_thingy_doodad(thingy_id, doodad_id, func)
>> with_next_doodad_id(func)
>> with_next_thingy_id(func)
>>
>> This function now has an infinite loop. Can you spot the reason?
>
> Not offhand, and to be completely honest, the original with the longer
> names was equally unreadable. I doubt this is the best way to do
> whatever the heck it is that this is supposed to do.
I agree. I think both are difficult to read. I find the first version
that I originally posted (using an imaginary anonymous function syntax)
much easier to understand.
I think I've made it pretty clear what this is supposed to do in my
earlier post to Fredrik, delineating each step of the communication
process. If you have a better way to do this, I'd certainly like to see it.
> Oh, and while I was typing my eyes fell on "funnc" misspelled above.
> Presumably that's your loop...
Yes, precisely. And because of a typo, the wrong callback gets passed,
causing it to use the same callback over and over. With anonymous
functions, there would be nothing to name, and therefore, nothing to
misspell.
> Spelling "func" as "_" would tend to avoid this as well, unless you have
> a keyboard that repeats keys accidentally.
Hrmmm. Well, it is less to type.
Dave
More information about the Python-list
mailing list