[Numpy-discussion] ndarray methods vs numpy module functions

Bob Dowling rjd4@cam.ac...
Mon Jun 23 03:31:24 CDT 2008

[ I'm new here and this has the feel of an FAQ but I couldn't find 
anything at http://www.scipy.org/FAQ .  If I should have looked 
somewhere else a URL would be gratefully received. ]

What's the reasoning behind functions like sum() and cumsum() being 
provided both as module functions (numpy.sum(data, axis=1)) and as 
object methods (data.sum(axis=1)) but other functions - and I stumbled 
over diff() - only being provided as module functions?

 >>> print numpy.__version__

 >>> data = numpy.array([[1,2,3],[4,5,6]])

 >>> numpy.sum(data,axis=1)
array([ 6, 15])

 >>> data.sum(axis=1)
array([ 6, 15])

 >>> numpy.diff(data,axis=1)
array([[1, 1],
        [1, 1]])

 >>> data.diff(axis=1)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'diff'

More information about the Numpy-discussion mailing list