[Numpy-discussion] Please help with subclassing numpy.ndarray
Tue Feb 6 07:14:13 CST 2007
On 2/6/07, Sturla Molden <email@example.com> 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 attribute, one has to temporarily define it
> as a class attribute? What happens if there is a thread switch between
> __new__ and __array_finalize__? This design is not thread safe and can
> produce strange race conditions.
> IMHO, the preferred way to set an instance attribute is to use __init__
> method, which is the 'Pythonic' way to do it.
> 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__.
More information about the Numpy-discussion