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


Groups > comp.lang.python > #99583

Re: What does a list comprehension do

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: What does a list comprehension do
Date 2015-11-26 17:36 +0200
Organization A noiseless patient Spider
Message-ID <87k2p43h8e.fsf@elektro.pacujo.net> (permalink)
References (4 earlier) <mailman.132.1448538769.20593.python-list@python.org> <87k2p4ex5x.fsf@elektro.pacujo.net> <mailman.138.1448544914.20593.python-list@python.org> <87fuzseuee.fsf@elektro.pacujo.net> <mailman.141.1448548095.20593.python-list@python.org>

Show all headers | View raw


Antoon Pardon <antoon.pardon@rece.vub.ac.be>:

>     [ <expression> for <var> in <iter> ]
>
> would implicitly be rewritten as follows:
>
>     [ (lambda <var>: <expression>)(<var>) for <var> in <iter>]

Funny enough, that's how "list comprehensions" are created in Scheme:

   (map (lambda (i)
          (lambda (x) (* i x)))
        '(0 1 2 3))))

> There would no change on how lambdas work or functions or closures.

First of all, it's weird to spend any effort in trying to alter a very
special case. I don't recall having to generate a list of such
functions. In fact, I barely ever use lambda in Python; explicit def
statements are much more pleasing to the eye and are not restricted to
simple expressions.

Secondly, you'd lose the nice symmetry between for statements and
comprehensions/generators. For example:

   ( lambda x: i * x for i in range(4) )

corresponds to:

    for i in range(4):
        yield lambda x: i * x

Would you embed an extra lambda there, too?

How about:

    i = 0
    while i < 4:
        yield lambda x: i * x
        i += 1

or:

    for i in range(4):
        def f(x):
            return i * x
        yield f


Marko

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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