Python complaints

skaller skaller at maxtal.com.au
Mon Dec 13 16:36:35 EST 1999


Ivan Van Laningham wrote:
 
> OK, I can understand the desire to eliminate lambda.  

	Well, what is it?

>But there are a
> couple of points I'm not clear on. To illustrate, I offer an example.

> 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;-)

	I think you misread a previous post.
List comprehensions don't replace lambda, they replace map:

	assert map(f,seq) == [f(x) for x in seq]

[I'll try adding that to Viper .. even better, if a lazy
version can be done ..]

	Lambda can be replaced by using ordinary named functions.
That is, it is entirely unnecessary already. It's just convenient,
it saves cluttering up code.
 
> 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 just define the function using 'def': replace

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

	by

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

In fact, for syntactic reasons, lambdas (in C and J Python)
are restricted compared to def -- they can only contain
expressions, whereas def allows statements.

-- 
John Skaller, mailto:skaller at maxtal.com.au
10/1 Toxteth Rd Glebe NSW 2037 Australia
homepage: http://www.maxtal.com.au/~skaller
voice: 61-2-9660-0850



More information about the Python-list mailing list