Re: question about numpy, subclassing, and a DeprecationWarning

Discussion in 'Python' started by Robert Kern, Jun 27, 2012.

  1. Robert Kern

    Robert Kern Guest

    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
    Robert Kern, Jun 27, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. drife
    Replies:
    1
    Views:
    340
    Travis E. Oliphant
    Mar 1, 2006
  2. Duncan Smith
    Replies:
    3
    Views:
    395
    Duncan Smith
    Apr 25, 2007
  3. Replies:
    2
    Views:
    468
    Robert Kern
    Nov 13, 2007
  4. W. eWatson
    Replies:
    2
    Views:
    896
    W. eWatson
    Nov 23, 2009
  5. Tom Kacvinsky

    Installing numpy over an older numpy

    Tom Kacvinsky, Jun 15, 2012, in forum: Python
    Replies:
    1
    Views:
    319
    Miki Tebeka
    Jun 15, 2012
Loading...

Share This Page