[Numpy-discussion] ndarray methods vs numpy module functions

Robert Kern robert.kern@gmail....
Tue Jun 24 12:30:06 CDT 2008

On Tue, Jun 24, 2008 at 02:33, Bob Dowling <rjd4@cam.ac.uk> wrote:
>> There is not supposed to be a one-to-one correspondence between the
>> functions in numpy and the methods on an ndarray. There is some
>> duplication between the two, but that is not a reason to make more
>> duplication.
> I would make a plea for consistency, to start with.

It's way too late to make changes like this.

> Those of us who write in an OO style are required to switch backwards
> and forwards between OO and not-OO, or to abandon OO altogether in our
> NumPy code.  Neither is an attractive option.

OO does not mean "always use methods."

> The reason I tripped over this is that I am currently writing a course
> which introduces students to NumPy.  I am going to be asked this
> question from the audience.  As yet I don't have any answer except
> "history".

Well, "history," usually along with "it seemed like a good idea at the
time," are valid reasons for things to continue to exist in any
nontrivial software project with a userbase. Your students will need
to learn this if they use software.

If you want a slightly better answer, the implementation of many of
the C functions were somewhat easier to do as methods on ndarray than
separate functions particularly since numpy.ndarray has subclasses.
The functions could then be implemented similar to the following:

  def myfunc(a):
      return asanyarray(a).myfunc()

One thing you will notice about numpy.diff() is that it is a pure
Python function rather than a C function, so it's certainly not going
to be a method on ndarray.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco

More information about the Numpy-discussion mailing list