[Numpy-discussion] Easter Egg or what I am missing here?

Siegfried Gonzi siegfried.gonzi at ed.ac.uk
Thu May 22 01:35:45 EDT 2014


On 22/05/2014 00:37, numpy-discussion-request at scipy.org wrote:
> Message: 4 Date: Wed, 21 May 2014 18:32:30 -0400 From: Warren 
> Weckesser <warren.weckesser at gmail.com> Subject: Re: [Numpy-discussion] 
> Easter Egg or what I am missing here? To: Discussion of Numerical 
> Python <numpy-discussion at scipy.org> Message-ID: 
> <CAGzF1udkdAP+YD2SQy9cCA6Rm4ZzfyjJzFv0TiEEsv_DRiVvew at mail.gmail.com> 
> Content-Type: text/plain; charset=UTF-8 On 5/21/14, Siegfried Gonzi 
> <siegfried.gonzi at ed.ac.uk> wrote:
>> >Please would anyone tell me the following is an undocumented bug
>> >otherwise I will lose faith in everything:
>> >
>> >==
>> >import numpy as np
>> >
>> >
>> >years = [2004,2005,2006,2007]
>> >
>> >dates = [20040501,20050601,20060801,20071001]
>> >
>> >for x in years:
>> >
>> >      print 'year ',x
>> >
>> >      xy =  np.array([x*1.0e-4 for x in dates]).astype(np.int)
>> >
>> >      print 'year ',x
>> >==
>> >
>> >Or is this a recipe to blow up a power plant?
>> >
> This is a "wart" of Python 2.x.  The dummy variable used in a list
> comprehension remains defined with its final value in the enclosing
> scope.  For example, this is Python 2.7:
>
>>>> >>>x = 100
>>>> >>>w = [x*x for x in range(4)]
>>>> >>>x
> 3
>
>
> This behavior has been changed in Python 3.  Here's the same sequence
> in Python 3.4:
>
>>>> >>>x = 100
>>>> >>>w = [x*x for x in range(4)]
>>>> >>>x
> 100
>
>
> Guido van Rossum gives a summary of this issue near the end of this
> blog:http://python-history.blogspot.com/2010/06/from-list-comprehensions-to-generator.html
>
> Warren
>
>
>

[I still do not know how to properly use the reply function here. I 
apologise.]

Hi all and thanks to all the respondes.

I think I would have expected my code to be behaving like you said 
version 3.4 will do.

I would never have thought 'x' is being changed during execution. I took 
me nearly 2 hours in my code to figure out what was going on (it was a 
lenghty piece of code an not so easy to spot).

Siegfried

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.




More information about the NumPy-Discussion mailing list