Python complaints

Greg Ewing greg.ewing at compaq.com
Wed Dec 15 03:36:44 EST 1999


Ivan Van Laningham wrote:
> 
> 1)  In the 'tm.add_command(...)' line, how would list comprehensions
> replace the 'command=lambda m=elements[ne]:setimage(m)' ?  How would
> they work?  Please explain for bears of very small mind;-)

They wouldn't. They're a replacement for map with a lambda
argument, not lambdas in general.

> 2)  In the Pythonian world of today (or is this the "Postpythonian
> world?"), how would one avoid the use of lambda and still use only one
> callback to handle every constructed entry in the menus?

You can always replace a lambda with a nested function
definition, with no loss of functionality. In your example,

        for j in range(lim):
            ne = (10 * i) + j
            def callback(m=elements[ne]):
               setimage(m)
            tm.add_command(label=elements[ne],
                command=callback)

The only thing lambdas buy you is less lines of code, if the 
function body happens to be a single expression. (Some would 
say at the expense of clarity.)

Greg



More information about the Python-list mailing list