Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99452

What does a list comprehension do (was: Late-binding of function defaults (was Re: What is a function parameter =[] for?))

From Antoon Pardon <antoon.pardon@rece.vub.ac.be>
Newsgroups comp.lang.python
Subject What does a list comprehension do (was: Late-binding of function defaults (was Re: What is a function parameter =[] for?))
Date 2015-11-25 14:51 +0100
Message-ID <mailman.73.1448459485.20593.python-list@python.org> (permalink)
References <CAPTjJmpwjWnF=d6mpgbKS1biVLoR4APutgyH0n9t6CJ=Kh4dCg@mail.gmail.com> <877fldnm9z.fsf@handshake.de>

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

What does a list comprehension do (was: Late-binding of function defaults (was Re: What is a function parameter =[] for?)) Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-25 14:51 +0100
  Re: What does a list comprehension do (was: Late-binding of function defaults (was Re: What is a function parameter =[] for?)) Nobody <nobody@nowhere.invalid> - 2015-11-26 11:13 +0000
    Re: What does a list comprehension do Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-26 12:52 +0100
      Re: What does a list comprehension do Marko Rauhamaa <marko@pacujo.net> - 2015-11-26 14:56 +0200
        Re: What does a list comprehension do Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-26 14:33 +0100
          Re: What does a list comprehension do Marko Rauhamaa <marko@pacujo.net> - 2015-11-26 15:56 +0200
            Re: What does a list comprehension do Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-26 15:26 +0100
              Re: What does a list comprehension do Marko Rauhamaa <marko@pacujo.net> - 2015-11-26 17:36 +0200
                Re: What does a list comprehension do Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-11-26 20:18 +0100
              Re: What does a list comprehension do Jussi Piitulainen <harvest@is.invalid> - 2015-11-26 18:11 +0200

csiph-web