[Numpy-discussion] Multiple inheritance from ndarray

Charlie Moad cwmoad at gmail.com
Wed Feb 22 12:02:04 EST 2006

Since no one has answered this, I am going to take a whack at it. 
Experts feel free to shoot me down.

Here is a sample showing multiple inheritance with a mix of old style
and new style classes.  I don't claim there is any logic to the code,
but it is just for demo purposes.
from numpy import *

class actImage:
   def __init__(self, colorOrder='RGBA'):
      self.colorOrder = colorOrder

class Image(actImage, ndarray):
   def __new__(cls, shape=(1024,768), dtype=float32):
      return ndarray.__new__(cls, shape=shape, dtype=dtype)

x = Image()
assert isinstance(x[0,1], float32)
assert x.colorOrder == 'RGBA'

Running "help(ndarray)" has some useful info as well.

- Charlie

On 2/19/06, Robert Lupton <rhl at astro.princeton.edu> wrote:
> I have a swig extension that defines a class that inherits from
> both a personal C-coded image struct (actImage), and also from
> Numeric's UserArray.  This works very nicely, but I thought that
> it was about time to upgrade to numpy.
> The code looks like:
> from UserArray import *
> class Image(UserArray, actImage):
>      def __init__(self, *args):
>          actImage.__init__(self, *args)
>          UserArray.__init__(self, self.getArray(), 'd', copy=False,
> savespace=False)
> I can't figure out how to convert this to use ndarray, as ndarray
> doesn't
> seem to have an __init__ method, merely a __new__.
> So what's the approved numpy way to handle multiple inheritance?
> I've a nasty
> idea that this is a python question that I should know the answer to,
> but I'm
> afraid that I don't...
>                                 R
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
> for problems?  Stop!  Download the new AJAX search engine that makes
> searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion

More information about the NumPy-Discussion mailing list