[Python-Dev] PEP 8: Discourage named lambdas?

"Martin v. Löwis" martin at v.loewis.de
Sun May 4 08:13:08 CEST 2008


> I guess that's inevitable (given lambda's existence... and human
> nature;-) and about on the same plane as another hatefully redundant
> construct I find myself having to beat upon over and over in code
> reviews:
> 
> if <expression>:
>     result = True
> else:
>     result = False
> return result
> 
> vs the simple "return <expression>" [[or bool(<expression>) if it's
> actually mandatory to return a bool and <expression> can't be relied
> upon to produce one]].

Indeed, I think these are the same phenomenons. If lambdas where not
available, people would, instead of writing

   Thread(target = lambda: foo())

write

   def target():
       foo()
   Thread(target = target)

Your example above demonstrates that the boolean type is a concept that
is *very* difficult to grasp (not the notion of boolean expressions,
which are easy to understand - it's the boolean *type* that is
difficult, i.e. that a boolean expression can be used not just in
conditional statements, but also assigned to variables, returned, etc.).

When I ask students in the exam to rewrite your code above (and assuming
they use Java), so that "it is a single line", they typically write

   return <expression> ? true : false;

Likewise, apparently, the notion of generic callables is difficult
to understand. Unless you have written it yourself, you won't recognize
an expression as callable.

Regards,
Martin


More information about the Python-Dev mailing list