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



> Thanks,
> 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
>
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to