sometype.__new__ and C subclasses

James Porter porterj at alum.rit.edu
Sun May 2 18:06:34 EDT 2010


On 5/2/2010 3:58 PM, Robert Kern wrote:
> Well, I think we can change zeros_like() and the rest to work around
> this issue. Can you bring it up on the numpy mailing list?
>
> def zeros_like(a):
> if isinstance(a, ndarray):
> res = numpy.empty(a.shape, a.dtype, order=a.flags.fnc)
> res.fill(0)
> res = res.view(type(a))
> return res
> ...

I'm having difficulty posting to the NumPy list (both via gmane and 
email) so I'm just going to put this here so it doesn't get lost. 
zeros_like probably needs to call __array_finalize__ for this to work 
properly (it'll cause a segfault for me otherwise):

      def zeros_like(a):
          if isinstance(a, ndarray):
              res = numpy.zeros(a.shape, a.dtype, order=a.flags.fnc)
              res = res.view(type(a))
              res.__array_finalize__(a)
              return res
          ...

- Jim




More information about the Python-list mailing list