Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-07 Thread Travis Oliphant
Sturla Molden wrote: >>Good point. I guess I thought the OP had tried that already. It turns >>out it works fine, too. >> >>The __array_finalize__ is useful if you want the attribute to be carried >>around when arrays are created automatically internally (after math >>operations for example). >>

Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-07 Thread Sturla Molden
> Good point. I guess I thought the OP had tried that already. It turns > out it works fine, too. > > The __array_finalize__ is useful if you want the attribute to be carried > around when arrays are created automatically internally (after math > operations for example). I too may be missing so

Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-07 Thread Travis Oliphant
Reggie Dugard wrote: >On Wed, 2007-02-07 at 14:36 -0700, Travis Oliphant wrote: > > >>Sturla Molden wrote: >> >> >> def __new__(cls,...) ... (H, edges) = numpy.histogramdd(..) cls.__defaultedges = edges def __array_finalize__(self, obj): if not hasa

Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-07 Thread Reggie Dugard
On Wed, 2007-02-07 at 14:36 -0700, Travis Oliphant wrote: > Sturla Molden wrote: > > >>def __new__(cls,...) > >> ... > >>(H, edges) = numpy.histogramdd(..) > >>cls.__defaultedges = edges > >> > >>def __array_finalize__(self, obj): > >>if not hasattr(self, 'edges'): > >>sel

Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-07 Thread Travis Oliphant
Sturla Molden wrote: >>def __new__(cls,...) >> ... >>(H, edges) = numpy.histogramdd(..) >>cls.__defaultedges = edges >> >>def __array_finalize__(self, obj): >>if not hasattr(self, 'edges'): >>self.edges = self.__defaultedges >> >> > >So in order to get an instance attr

Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-06 Thread Sturla Molden
> I don't pretend to know all the inner workings of subclassing, but I > don't think that would work, given the following output: > In [6]: x+y > This is where __array_finalize__ is called > Out[6]: MyArray([4, 5, 6]) Why is not __new__ called for the return value of x + y? Does it call __new__

Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-06 Thread Sturla Molden
> Yes using __init__ to set an instance attribute is the Pythonic way to > do this. However, I calculate/create the data in __new__. The data > is unavailable to __init__. The signatures of __new__ and __init__ is: def __new__(cls, *args, **kwds) def __init__(self, *args, **kwds) If __new__ h

Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-06 Thread Jeremy Conlin
On 2/6/07, Sturla Molden <[EMAIL PROTECTED]> wrote: > > > def __new__(cls,...) > > ... > > (H, edges) = numpy.histogramdd(..) > > cls.__defaultedges = edges > > > > def __array_finalize__(self, obj): > > if not hasattr(self, 'edges'): > > self.edges = self.__defaultedges >

Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-06 Thread Stefan van der Walt
On Tue, Feb 06, 2007 at 01:06:37PM +0100, Sturla Molden wrote: > > > def __new__(cls,...) > > ... > > (H, edges) = numpy.histogramdd(..) > > cls.__defaultedges = edges > > > > def __array_finalize__(self, obj): > > if not hasattr(self, 'edges'): > > self.edges = self.__de

Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-06 Thread Sturla Molden
> def __new__(cls,...) > ... > (H, edges) = numpy.histogramdd(..) > cls.__defaultedges = edges > > def __array_finalize__(self, obj): > if not hasattr(self, 'edges'): > self.edges = self.__defaultedges So in order to get an instance attribute, one has to temporarily defi

Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-05 Thread Jeremy Conlin
On 2/5/07, Pierre GM <[EMAIL PROTECTED]> wrote: > On Monday 05 February 2007 11:32:22 Jeremy Conlin wrote: > > Thanks for clarifying that. I didn't understand what the > > __array_finalize__ did. > > That means I should clarify some points on the wiki, then. > A good exercise is to put some tempor

Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-05 Thread Pierre GM
On Monday 05 February 2007 11:32:22 Jeremy Conlin wrote: > Thanks for clarifying that. I didn't understand what the > __array_finalize__ did. That means I should clarify some points on the wiki, then. A good exercise is to put some temporary comments in your code in __new__ and __array_finalize

Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-05 Thread Jeremy Conlin
On 2/4/07, Pierre GM <[EMAIL PROTECTED]> wrote: > On Sunday 04 February 2007 20:22:44 Jeremy Conlin wrote: > > I have subclassed the numpy.ndarray object, but need some help setting > > some attributes. I have read http://scipy.org/Subclasses but it > > doesn't provide the answer I am looking for.

Re: [Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-04 Thread Pierre GM
On Sunday 04 February 2007 20:22:44 Jeremy Conlin wrote: > I have subclassed the numpy.ndarray object, but need some help setting > some attributes. I have read http://scipy.org/Subclasses but it > doesn't provide the answer I am looking for. Actually, yes: In the example given in http://scipy.or

[Numpy-discussion] Please help with subclassing numpy.ndarray

2007-02-04 Thread Jeremy Conlin
I have subclassed the numpy.ndarray object, but need some help setting some attributes. I have read http://scipy.org/Subclasses but it doesn't provide the answer I am looking for. I create an instance of the class in my __new__ method as: import numpy class MyClass(numpy.ndarray): __new__(self,