[Numpy-discussion] use of concatenate with subclass of ndarray
Bryce Hendrix
bhendrix at enthought.com
Wed Mar 28 12:42:52 EDT 2007
Thanks Pierre, works like a charm. One question though, how is defining
a class attribute in __new__ any more thread-safe?
Bryce
Pierre GM wrote:
> On Tuesday 27 March 2007 20:08:04 Bryce Hendrix wrote:
>
>> We have a class which is a subclass of ndarray which defines
>> __array_finalize__ to add an attribute. The class looks something like
>> this:
>>
>
> Ahah, we ran into this problem a few months ago:
> You should not initialize your "units" attribute in the __new__ method, as
> it's not thread-safe first, but more important as you will lose it if you try
> to get a view of your new object (because in that case, __new__ is not
> called).
> Instead, try something like that:
>
> def __array_finalize__(self, obj):
> self.units = getattr(obj, 'units', yourdefaultunit)
>
> where your defaultunit is the value you would use in __new__. True,
> yourdefaultunit may change, and even be lost if there's a break between
> __new__ and __array_finalize__, but that should work. Lemme know.
>
> Note that things are always tricky w/ concatenate: when you concatenate your
> UnitArray, you should have a UnitArray, OK. But if you mix UnitArray and
> ndarray ? What should take precedence ?
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070328/971439e3/attachment.html>
More information about the NumPy-Discussion
mailing list