Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #24547
| From | Robert Kern <robert.kern@gmail.com> |
|---|---|
| Subject | Re: question about numpy, subclassing, and a DeprecationWarning |
| Date | 2012-06-27 22:57 +0100 |
| References | <CAEk9e3qQQL2RZZZHQZdri9B026Av2HWniMBbP6KbgARQMO2c+A@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1568.1340834275.4697.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: question about numpy, subclassing, and a DeprecationWarning Robert Kern <robert.kern@gmail.com> - 2012-06-27 22:57 +0100
csiph-web