On 22/05/2014 00:37, numpy-discussion-requ...@scipy.org wrote:
> Message: 4 Date: Wed, 21 May 2014 18:32:30 -0400 From: Warren 
> Weckesser <warren.weckes...@gmail.com> Subject: Re: [Numpy-discussion] 
> Easter Egg or what I am missing here? To: Discussion of Numerical 
> Python <numpy-discussion@scipy.org> Message-ID: 
> <cagzf1udkdap+yd2sqy9cca6rm4zzfyjjzfv0tieesv_driv...@mail.gmail.com> 
> Content-Type: text/plain; charset=UTF-8 On 5/21/14, Siegfried Gonzi 
> <siegfried.go...@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.

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to