Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Antoon Pardon Newsgroups: comp.lang.python Subject: Re: What does a list comprehension do Date: Thu, 26 Nov 2015 14:33:59 +0100 Lines: 65 Message-ID: References: <877fldnm9z.fsf@handshake.de> <87k2p4ex5x.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de QPXh6BkURWioQARoZAZpqwOnMp9p/gf507zYqX9la/ww== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.018 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'received:134': 0.05; 'subject:skip:c 10': 0.07; 'works.': 0.07; '(0,': 0.09; 'python.': 0.11; 'def': 0.13; 'lambda': 0.16; 'python3.': 0.16; 'received:ac.be': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; "wouldn't": 0.16; 'proposed': 0.20; 'minor': 0.22; 'header :In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'subject:list': 0.26; 'change,': 0.27; 'function': 0.28; 'behaviour': 0.29; 'convert': 0.29; 'there.': 0.30; 'code': 0.30; 'received:be': 0.30; 'run': 0.33; 'clarify': 0.33; 'list': 0.34; 'could': 0.35; 'propose': 0.35; 'should': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'turned': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'course': 0.62; '>>>>>': 0.66; 'results': 0.66; 'talking': 0.67; 'worth': 0.67; 'about,': 0.84; 'pardon': 0.84; 'schreef': 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnIJAPcIV1aGuA9G/2dsb2JhbABehFcBJb93hg8CgX0BAQEBAQGFQAEBBCNVEQsaAgUWCwICCQMCAQIBRRMGAgKIKq4XjBWEHAEBCAIhgQGFU4R+hSeCToFEBZZXjTWBXIc0j1yDcmOEBXEBhV8BAQE User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Icedove/31.8.0 In-Reply-To: <87k2p4ex5x.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99574 Op 26-11-15 om 13:56 schreef Marko Rauhamaa: > Antoon Pardon : > >> Personnaly I would prefer: >> >>>>> q = [(lambda i: lambda x: i * x)(i) for i in range(4)] >>>>> q[0](1), q[3](1) >> (0, 3) >> >> And this is where I ask whether it would be worth the effort to change >> the behaviour of python. > Don't go there. > > Consider: > > q = [] > n = 0 > x = "hello" > > for i in range(4): > def stepper(): > global n > n += 1 > return i * x > q.append(stepper) > > print(n) > print(q[1]()) > print(n) > x = "there" > print(q[3]()) > print(n) > > which prints: > > 0 > hellohellohello > 1 > theretherethere > 2 > > after your change, you'd get: > > 0 > hello > 0 > hellohellohello > 0 I don't understand. What I propose would be a minor change in how list comprehension works. I don't see how your example can be turned into a list comprehension. I can of course put the stepper function out of the for loop and then convert the for loop into a list comprehension. The results in that case is that my proposed change wouldn't effect the results in python2 and that the code wouldn't run in python3. So could you clarify what list comprehension you are talking about, that you think would cause trouble, should my proposal be adopted. -- Antoon.