Op 20-11-15 om 08:49 schreef dieter:
> In addition, the last few days have had two discussions in this list
> demonstrating the conceptial difficulties of late binding -- one of them:
>
>       Why does "[lambda x: i * x for i in range(4)]" gives
>       a list of essentially the same functions?

Can you (or someone else) explain what a list comprehension is equivallent of.
Especially in python3.

Take this simple list comprhesion:

[x * x for x in range(10)]

what would this be equivallent of? Something like:

def lch1():
  ls = []
  for x in range(10):
    ls.append(x * x)
  return ls

Or more something like:

def lch2():
  def expr(x):
    return x * x

  ls = []
  for x in range(10):
    ls.append(expr(x))
  return ls

For this example it doesn't make a difference but for the example above
it would become important.

def lch3():
  ls = []
  for i in range(4):
    ls.append(lambda x: i * x)
  return ls

versus

def lch4():
  def expr(i):
    return lambda x: i * x

  ls = []
  for i in range(4)
    ls.append(expr(i))
  return ls

Now from the result we get I expect list comprehensions to work more
like lch1 and lch3 rather than lch2 and lch4. But I can understand
people who think of the expression as a function of the variable that
is iterated over.

Am I missing something? Would it be worthwile considering changing
this behaviour?

-- 
Antoon.

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to