sometype.__new__ and C subclasses

Robert Kern robert.kern at gmail.com
Sun May 2 16:58:27 EDT 2010


On 2010-05-02 15:03 , James Porter wrote:

> And then in Python:
>
> A = iMesh.Array(numpy.array([1,2,3,4,5]), instance=mesh)
> numpy.zeros_like(A) # fails here
>
> Inside NumPy, zeros_like looks like this (there's a bit more than this,
> but it's irrelevant to this problem):
>
> def zeros_like(a):
> if isinstance(a, ndarray):
> res = ndarray.__new__(type(a), a.shape, a.dtype,
> order=a.flags.fnc)
> res.fill(0)
> return res

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
     ...

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list