Hacker News new | past | comments | ask | show | jobs | submit login

This is rubbish. From comments right there on that site, Python and many other languages "leak" the iterated result, and combined with closures this doesn't work. The right way to do it is there:

  >>> fs = [(lambda n, j=i: j + n) for i in range(10)]
  >>> fs[3](4)
  7
Anyway, Python 3.0 got rid of lambda. It complicates things and always there's a way to do it more pythonic.

Note for lisp knee-jerkers: I don't have anything against anonymous functions. In Python, 3rd party code is harder to understand if it has lambdas all over the place. At least for me.

HN: This is yet another example of front page trash.

EDIT:

Seems this teacher guy is not even recognizing his mistake. Now it's a "design error."

  While I could accept the explanation about how closures capture the location
  and not the value, this really is a design error. The loop variable ough to be
  local to the loop. But I suppose I get what I deserve for using a dynamic language.
From http://www.python.org/doc/2.5.2/ref/naming.html, it is either a module, function, class definition, script (file|command|eval), or the expression fed to input() (rare.)

  4.1 Naming and binding

  Names refer to objects. Names are introduced by name binding operations.
  Each occurrence of a name in the program text refers to the binding of that
  name established in the innermost function block containing the use.

  A block is a piece of Python program text that is executed as a unit.
  The following are blocks: a module, a function body, and a class definition. 
  A script file [...] is a code block. A script command [...] is a code
  block. The file read by the built-in function execfile() is a code block.
  The string argument passed to the built-in function eval() and to the
  exec statement is a code block. The expression read and evaluated by the
  built-in function input() is a code block.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: