a = b = 1 just syntactic sugar?

Moshe Zadka m at moshez.org
Mon Jun 9 13:30:55 CEST 2003

On 09 Jun 2003, martin at v.loewis.de (Martin v. =?iso-8859-15?q?L=F6wis?=) wrote:

> To summarize, you can only use print, raise, yield, and exec in a
> meaningful way. Why would I want to have any of these in a lambda
> expression?

Well, I don't know about yield or exec, but I actually *would* like to
print from lambdas, I do it a lot when debugging Twisted code:

from twisted.web client
a = client.getPage("http://www.python.org/")
a.addCallback(lambda x: print x)
a.addCallback(lambda _: reactor.stop())

is an example.

Likewise, raising exceptions is often useful in callbacks too:

d.addErrback(lambda e: raise xmlrpc.Fault(4003))

would allow me to convert from any exception to the xmlrpc.Fault exception.

Currently, we have in Twisted the util.println function, which emulates
print, so the first one I could do with a.addCallback(util.println).
In the same vein, it would not be hard to write

def raise_(ex):
    raise ex

so I can d.addErrback(raise_, xmlrpc.Fault(4003)).

All this should not be taken to mean that I support "statements in lambdas"
proposal. I would much prefer to have builtins which correspond to print
and raise (perhaps operator.__print__ and operator.__raise__?)
Moshe Zadka -- http://moshez.org/
Buffy: I don't like you hanging out with someone that... short.
Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
Agile Programming Language -- http://www.python.org/

More information about the Python-list mailing list