[Numpy-discussion] use of concatenate with subclass of ndarray
Thu Mar 29 11:04:51 CDT 2007
I spoke too soon, this code fails with the example you gave:
unit_ary_1 = UnitArray(numpy.array((1,2,3)), units=meters)
unit_ary_2 = UnitArray(numpy.array((1,2,3)), units=meters)
# create another instance with different units. This instance
# is never used, but calls __new__ with different units
unit_ary_3 = UnitArray(numpy.array((1,2,3)), units=feet)
new_unit_ary = numpy.concatenate([unit_ary_1, unit_ary_2])
Any other ideas?
Pierre GM wrote:
> On Wednesday 28 March 2007 12:42:52 Bryce Hendrix wrote:
>> Thanks Pierre, works like a charm. One question though, how is defining
>> a class attribute in __new__ any more thread-safe?
> It's not, of course, and that's why it shouldn't be used. However, it's quite
> convenient and easier to use, and do you really need thread-safe objects ?
> In your example, if "yourdefaultunit" takes some simple value, you could use
> that value directly instead of the class attributes, which could be slightly
> messier to read but definitely thread-safe.
> The second aspect about initialization is that when a ndarray is viewed as one
> of its subclasses, the actual memory space has already been allocated, so
> there's no call to __new__. Instead, you have to rely on __array_finalize__
> to initialize the attributes specific to your subclass.
> Numpy-discussion mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Numpy-discussion