Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Marko Rauhamaa Newsgroups: comp.lang.python Subject: Re: What does a list comprehension do Date: Thu, 26 Nov 2015 14:56:58 +0200 Organization: A noiseless patient Spider Lines: 57 Message-ID: <87k2p4ex5x.fsf@elektro.pacujo.net> References: <877fldnm9z.fsf@handshake.de> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="b7cb1518d23ec19d482dcc9c31d30fdd"; logging-data="2490"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/V41dCiRSxNuUynvo7zI9B" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:jqHzr5oK4LXQAG5ZvdRDCnbwn7w= sha1:UbRDj7NJsBf1fxRb7WRsUqWIr+s= Xref: csiph.com comp.lang.python:99571 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 > It also seems that people who try this for the first time are > surprised with what they get and seem to expect there list > comprehension to act as if they had written the second version. I might trip over that one, too. Still, nothing should be changed. Marko