sometype.__new__ and C subclasses

Robert Kern robert.kern at gmail.com
Sun May 2 14:43:08 EDT 2010


On 2010-05-02 12:48 , James Porter wrote:
> On 5/2/2010 4:34 AM, Carl Banks wrote:
>> Why don't you use mysubtype.__new__(mysubtype,...)?
>>
>> If you wrote mysubtype in C, and defined a different tp_new than
>> ndarray, then this exception will trigger. And it ought to; you don't
>> want to use ndarray's tp_new to create an object of your subclass, if
>> you've defined a different tp_new.
>
> Unfortunately, I can't do that, since that call is in NumPy itself and
> it's part of their "standard" way of making instances of subclasses of
> ndarray. Functions like numpy.zeros_like use ndarray.__new__(subtype,
> ...) to create new arrays based on the shape of other arrays.
>
> The Python version of the subclass is shown here:
> <http://docs.scipy.org/doc/numpy/user/basics.subclassing.html#slightly-more-realistic-example-attribute-added-to-existing-array>,
> and I'm trying to write something pretty similar in C. I'm trying to
> stay in C since everything else is in C, so it's easier to stay in C
> then to jump back and forth all the time.
>
> Maybe the real answer to this question is "NumPy is doing it wrong" and
> I should be on their list; still, it seems strange that the behavior is
> different between Python and C.

Perhaps things would be clearer if you could post the C code that you've written 
that fails. So far, you've only alluded at what you are doing using 
Python-syntax examples.

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