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

On 22/05/2014 00:37, numpy-discussion-request@scipy.org wrote:
Message: 4 Date: Wed, 21 May 2014 18:32:30 -0400 From: Warren Weckesser <warren.weckesser@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_DRiVvew@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 On 5/21/14, Siegfried Gonzi <siegfried.gonzi@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-gener...
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.

I agree; this 'wart' has also messed with my code a few times. I didn't find it to be the case two years ago, but perhaps I should reevaluate if the scientific python stack has sufficiently migrated to python 3. On Thu, May 22, 2014 at 7:35 AM, Siegfried Gonzi <siegfried.gonzi@ed.ac.uk>wrote:
On 22/05/2014 00:37, numpy-discussion-request@scipy.org wrote:
Message: 4 Date: Wed, 21 May 2014 18:32:30 -0400 From: Warren Weckesser <warren.weckesser@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_DRiVvew@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 On 5/21/14, Siegfried Gonzi <siegfried.gonzi@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-gener...
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
participants (2)
-
Eelco Hoogendoorn
-
Siegfried Gonzi