numpy.product vs numpy.prod vs ndarray.prod numpy.product vs numpy.prod vs ndarray.prod python python

numpy.product vs numpy.prod vs ndarray.prod


As of the master branch today (1.15.0), np.product just uses np.prod, and may be deprecated eventually. See MAINT: Remove duplicate implementation for aliased functions. #10653.

And np.prod and ndarray.prod both end up calling umath.multiply.reduce, so there is really no difference between them, besides the fact that the free functions can accept array-like types (like Python lists) in addition to NumPy arrays.

Prior to this, like in NumPy 1.14.2, the documentation claimed np.product and np.prod were the same, but there were bugs because of the duplicated implementation that Parag mentions. i.e. Eric Weiser's example from #10651:

>>> class CanProd(object):        def prod(self, axis, dtype, out): return "prod"  >>> np.product(CanProd())<__main__.CanProd object at 0x0000023BAF7B29E8>>>> np.prod(CanProd())'prod'

So in short, now they're the same, and favor np.prod over np.product since the latter is an alias that may be deprecated.


This is what I could gather from the source codes of NumPy 1.14.0. For the answer relevant to the current Master branch (NumPy 1.15.0), see the answer of miradulo.

  • For an ndarray, prod() and product() are equivalent.

  • For an ndarray, prod() and product() will both call um.multiply.reduce().

  • If the object type is not ndarray but it still has a prod method, then prod() will return prod(axis=axis, dtype=dtype, out=out, **kwargs) whereas product will try to use um.multiply.reduce.

  • If the object is not an ndarray and it does not have a prod method, then it will behave as product().

  • The ndarray.prod() is equivalent to prod().

I am not sure about the latter part of your question regarding preference and readability.