Zen of Python

Fredrik Lundh fredrik at pythonware.com
Sat Jan 22 08:45:12 EST 2005

Alex Martelli wrote:

>> I do remember seeing some cute tricks (i.e. capable of becoming
>> idioms) that depend on the leakage.
> Sure, ``if [mo for mo in [myre.search(line)] if mo]: use(mo)`` and the
> like, used to simulate assign-and-test.

here's an old favourite:

    lambda x: ([d for d in [{}]], [d.setdefault(k.text or "", unmarshal(v)) for (k, v) in x], d)[2]

which, when all keys are unique, is a 2.1-compatible version of

    lambda x: dict([(k.text or "", unmarshal(v)) for k, v in x])

which illustrates why some lambdas are better written as functions.


More information about the Python-list mailing list