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


Groups > comp.lang.python > #99593

Re: What does a list comprehension do

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Antoon Pardon <antoon.pardon@rece.vub.ac.be>
Newsgroups comp.lang.python
Subject Re: What does a list comprehension do
Date Thu, 26 Nov 2015 20:18:14 +0100
Lines 56
Message-ID <mailman.151.1448565531.20593.python-list@python.org> (permalink)
References <CAPTjJmpwjWnF=d6mpgbKS1biVLoR4APutgyH0n9t6CJ=Kh4dCg@mail.gmail.com> <877fldnm9z.fsf@handshake.de> <mailman.73.1448459485.20593.python-list@python.org> <pan.2015.11.26.11.13.43.440000@nowhere.invalid> <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> <87k2p43h8e.fsf@elektro.pacujo.net>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de edTCRWozPnWFPLADpd8nUAgPqKGlwoV38sx+Gm2LAysg==
Return-Path <antoon.pardon@rece.vub.ac.be>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'dev': 0.07; 'subject:skip:c 10': 0.07; 'alter': 0.09; 'corresponds': 0.09; 'semantics': 0.09; 'statements': 0.09; 'example:': 0.10; 'python.': 0.11; 'def': 0.13; 'intermediate': 0.15; 'weird': 0.15; '"list': 0.16; '(lambda': 0.16; 'expressions.': 0.16; 'lambda': 0.16; 'python;': 0.16; 'received:adsl-dyn.isp.belgacom.be': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'rewritten': 0.16; 'scope.': 0.16; 'suite.': 0.16; 'too?': 0.16; 'case.': 0.18; 'variable': 0.18; 'language': 0.19; 'all,': 0.20; 'embedding': 0.22; 'explicit': 0.22; 'variables.': 0.22; 'trying': 0.22; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'yield': 0.27; 'function': 0.28; 'embed': 0.29; 'implicitly': 0.29; 'sleep': 0.29; 'received:be': 0.30; 'language.': 0.32; 'maybe': 0.33; 'list': 0.34; 'follows:': 0.35; 'functions.': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'created': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'there,': 0.37; 'no,': 0.38; 'mean': 0.38; 'enough': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'ever': 0.60; 'your': 0.60; 'eye': 0.61; 'charset:windows-1252': 0.62; 'skip:n 10': 0.62; 'lose': 0.63; 'more': 0.63; 'within': 0.64; 'between': 0.65; 'fact,': 0.67; 'spend': 0.67; 'special': 0.73; 'introduce': 0.79; 'funny': 0.83; 'loose': 0.84; 'pardon': 0.84; 'received:195.238': 0.84; 'schreef': 0.84
X-Belgacom-Dynamic yes
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result A2CbAAC1WldW/9Xi9VENUYR9vgkBDYFkhg8CgWwUAQEBAQEBAYU/AQEDAXgGCwshFg8JAwIBAgFFEwYCAogiDaxhjHOEHAEBCAIBIIZUhH6FJ4QSAQSWV401gVyXEINyHwEBhEdxAYVfAQEB
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0
In-Reply-To <87k2p43h8e.fsf@elektro.pacujo.net>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99593

Show key headers only | View raw


Op 26-11-15 om 16:36 schreef Marko Rauhamaa:
> 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.

<shrug> Maybe it is small enough an effort but I wont loose any sleep over it
should the dev team have other priorities.

> 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?

No, not because that would be so troublesome in semantics
but because it would be annoying in python. Because in that
case you would embed a function (call) or introduce an
intermediate scope. Which would mean that if you want
to rebind a variable within the suite of the for loop
that is also used in other parts of the function/modules
you would have to declare that variable nonlocal/global at the
start of the suite. So it would be annoying plus that
it would be difficult to explain to people new to the
language.

Such an embedding would only be practical in a language where
you have to declare your variables.

-- 
Antoon.

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