[Numpy-discussion] use of concatenate with subclass of ndarray

Bryce Hendrix bhendrix at enthought.com
Thu Mar 29 12:04:51 EDT 2007


I spoke too soon, this code fails with the example you gave:

def test_concatenate(self):
  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])
  self.assertEqual(new_unit_ary.units, meters)

Any other ideas?

Bryce

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
> 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/20070329/3b8b50a0/attachment.html>


More information about the NumPy-Discussion mailing list