News for hackers. Stuff that matters.
Re:Common Python Mistakes by gejjaxxita on Thursday May 08, 2014 @11:29AM (#7715789)
#6 is really confusing. Whenever I encounter something like this my first reaction is that whenever possible such obscure components of a language should be avoided and more verbose/clear code used instead.

Programming languages are meant to be read as well as written, and someone relatively new to Python (and many who have used the language for a long time) is certain to get confused about the difference between:

   return [lambda x, i=i : i * x for i in range(5)]
and

   return [lambda x : i * x for i in range(5)]
Re:Common Python Mistakes by mguillech on Thursday May 08, 2014 @11:35AM (#7715828)
Agreed 100%, this type of constructions should be avoided in the first place in favor of more "readable" ones but this happens in a fair amount of code that I've seen (and I keep seeing).
Re:Common Python Mistakes by maxerickson on Thursday May 08, 2014 @11:41AM (#7715865)
Some of it seems to come from people cargo-culting their knowledge of anonymous and first class functions, so they end up believing that the only way to pass a function around is to construct it anonymously.
Re:Common Python Mistakes by nine_k on Thursday May 08, 2014 @03:05PM (#7717019)
In the particular case of constructing several similar functions that do essentially the same thing, lambda is a rather natural choice.

Being more explicit and less hacky can well be combined with staying true to functional style:

    from functools import partial
    
    mul = lambda x, y: x*y  # could use int.__mul__, too
    multipliers = [partial(mul, n) for n in range(5)]
It does the closure-capturing of n for you.
Re:Common Python Mistakes by maxerickson on Thursday May 08, 2014 @03:30PM (#7717160)
A function defined with def would work just the same there.

It would have a big ugly 'return' in it and be a few characters longer, but it would work the same, so I don't see what lambda brings to it.

Re:Common Python Mistakes by nine_k on Thursday May 08, 2014 @04:34PM (#7717503)
Both def and lambda would fail identically in this context.