Creating a new object of your own type in a C extension.

Heiko Wundram heikowu at ceosg.de
Sun May 30 04:46:44 EDT 2004


Hi all!

Just a small question concerning type creation in a C object: If I create a 
class which is overridable, I'd like to have that class create objects of 
it's overridden type, e.g. I have a class X which is implemented in C, which 
is wrapped in Python by a class Y, now I initialize a Y class, and call a 
member of X on that class, and I'd like the member of X to return an object 
of class Y. What is a correct way of doing this? I've not found any nice 
implementation hints in the documentation...

1) PyObject_New(Xstruct, self->ob_type (which should point to the type of Y))
2) self->ob_type->tp_new(self->ob_type,NULL,NULL)
3) anything else?

I don't see any of this being used in the Python object classes (e.g. 
overridden classes of int return int when e.g. __add__ is called, etc.).

Thanks for any help!

Heiko.




More information about the Python-list mailing list