[SciPy-dev] numpy changes generator behaviour
Fernando Perez
Fernando.Perez at colorado.edu
Mon Jan 9 13:07:40 CST 2006
David M.Cooke wrote:
> On Jan 9, 2006, at 09:01 , Christopher Fonnesbeck wrote:
>
>
>>I'm not sure this is intended, but it appears that numpy changes
>>the behaviour of the new python 2.4 generators. Here is what is
>>supposed to happen:
>>
>>
>>>>>sum(x*x for x in range(10))
>>
>>285
>>
>>But if I import numpy, and try again I get:
>>
>>
>>>>>from numpy import *
>>>>>sum(x*x for x in range(10))
>>
>><generator object at 0x5ae90>
>>
>>Why should it no longer sum?
>
>
> Because the function 'sum' is now the one from numpy, not the builtin
> one. (Numeric had 'sum' before it was added to Python.)
I know that in this case Numeric has historical precedence, but I'm wondering
if it wouldn't be a good idea to rename numpy.{sum,product} to asum/aproduct.
While I find it a bit unpleasant (and it is backwards-incompatible), I think
that staying clear of the python builtins is probably a good idea.
This would also allow people to easily use both the builtin sum and the numpy
one when they use 'import *', without having to jump through __builtin__ hoops
or pre-binding 'sum' to a local name.
We do, after all, live on top of the python language, so my take on this is
that we should be willing to accomodate our practices a little as the language
evolves. Since we are in this case making a big break, I think it's an
unusually good opportunity to do some compatibility-breaking cleanup work.
Along these lines, do we really need both prod and product?
In [8]: N.prod??
Type: function
Base Class: <type 'function'>
String Form: <function prod at 0x40590844>
Namespace: Interactive
File:
/home/fperez/usr/local/lib/python2.3/site-packages/numpy/core/oldnumeric.py
Definition: N.prod(a, axis=0)
Source:
def prod(a, axis=0):
"""Return the product of the elements along the given axis
"""
return asarray(a).prod(axis)
In [9]: N.product??
Type: function
Base Class: <type 'function'>
String Form: <function product at 0x405905dc>
Namespace: Interactive
File:
/home/fperez/usr/local/lib/python2.3/site-packages/numpy/core/oldnumeric.py
Definition: N.product(x, axis=0, dtype=None)
Source:
def product (x, axis=0, dtype=None):
"""Product of the array elements over the given axis."""
return asarray(x).prod(axis, dtype)
These two are nearly identical, let's get rid of one of them (less namespace
noise).
Cheers,
f
More information about the Scipy-dev
mailing list