Why are some numpy calls not implemented as methods?

I've always considered Python a very object oriented programming language. Lately I've been using numpy lot, and I'm starting to wonder why some things are implemented there only as functions, and not as methods of the numpy.array (or ndarray ) ndarray .

If we have a given array a , for example, you can do

 a = np.array([1, 2, 3]) np.sum(a) >>> 6 a.sum() >>> 6 

which seems just fine, but there are many challenges that don't work the same as in:

 np.amax(a) >>> 3 a.amax() >>> AttributeError: 'numpy.ndarray' object has no attribute 'amax' 

I find this confusing, unintuitive, and I see no reason. However, it may be good; maybe someone can just enlighten me.

+7
python numpy
source share
1 answer

When numpy was introduced as a successor to Numeric, many things that were just functions, not methods, were added as methods of type ndarray . At this particular moment, the ability to subclass an array type was a new feature. It was believed that creating some of these common function methods would allow subclasses to do the right thing more easily. For example, using .sum() as a method is useful to allow the masked array type to ignore masked values; you can write generic code that will work for both simple ndarray and masked arrays without any branching.

Of course, you cannot get rid of functions. A good feature of functions is that they will accept any object that can be forced into ndarray , for example, a list of numbers that will not have all ndarray methods. And the specific list of functions that have been added as methods cannot be comprehensive; This is not a very good OO development. It is not necessary that all trigger functions be added, for example, as methods.

The current list of methods was mainly chosen at the beginning of numpy development for those that we thought would be useful as methods from the point of view of a subclass or nationally convenient. With a lot of experience, we came to the conclusion that we added too many methods (seriously, there is no need for .ptp() be a method), and that subclassing ndarray usually a bad idea for reasons I won't be here.

Therefore, take a list of methods, usually a subset of the list of available functions, and np.amin() and np.amax() as small renames of the .min() and .min() methods to avoid smoothing the built-in functions.

+11
source share

All Articles