question about numpy, subclassing, and a DeprecationWarning
Robert Kern
robert.kern at gmail.com
Wed Jun 27 17:57:39 EDT 2012
On 6/27/12 10:02 PM, Jason Swails wrote:
> Hello,
>
> I'm running into an unexpected issue in a program I'm writing, and I was hoping
> someone could provide some clarification for me. I'm trying to subclass
> numpy.ndarray (basically create a class to handle a 3D grid). When I
> instantiate a numpy.ndarray, everything works as expected. When I call
> numpy.ndarray's constructor directly within my subclass, I get a deprecation
> warning about object.__init__ not taking arguments. Presumably this means that
> ndarray's __init__ is somehow (for some reason?) calling object's __init__...
>
> This is some sample code:
>
> >>> import numpy as np
> >>> class derived(np.ndarray):
> ... def __init__(self, stuff):
> ... np.ndarray.__init__(self, stuff)
> ...
> >>> l = derived((2,3))
> __main__:3: DeprecationWarning: object.__init__() takes no parameters
> >>> l
> derived([[ 8.87744455e+159, 6.42896975e-109, 5.56218818e+180],
> [ 1.79996515e+219, 2.41625066e+198, 5.15855295e+307]])
> >>>
>
> Am I doing something blatantly stupid? Is there a better way of going about
> this? I suppose I could create a normal class and just put the grid points in a
> ndarray as an attribute to the class, but I would rather subclass ndarray
> directly (not sure I have a good reason for it, though). Suggestions on what I
> should do?
numpy.ndarray does not have its own __init__(), just a __new__(). It's
__init__() is the same as object.__init__(), which takes no arguments.
[~]
|3> np.ndarray.__init__ is object.__init__
True
There is no need to call np.ndarray.__init__() explicitly.
http://docs.scipy.org/doc/numpy/user/basics.subclassing.html#a-brief-python-primer-on-new-and-init
You will also want to ask numpy questions on the numpy mailing list.
http://www.scipy.org/Mailing_Lists
Personally, I recommend not subclassing ndarray at all. It rarely works out well.
--
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 Python-list
mailing list