C API eqivalent of object.__new__

Alex Martelli aleaxit at yahoo.com
Fri Oct 1 09:10:11 EDT 2004


Jacek Generowicz <jacek.generowicz at cern.ch> wrote:

> Jacek Generowicz <jacek.generowicz at cern.ch> writes:
> 
> > How would one write object._new__(SomeType) in the C API ?
> 
> PyInstance_NewRaw(PyObject *class, [PyObject* dict])
> 
> seems to be the way to do this for classic classes. How would one do
> it for new-style classes?

You mean something like:
PyObject_CallMethod( (PyObject*)&PyBaseObject_Type,
                                "__new__", "O", clas);
?
 
> Also, how would one create a new-style class "dynamically" in the C
> API? IOW, what's the new-style equivalent of PyClass_New? or, put
> another way, how would you write
> 
>   type(name, (), dict)
> 
> in the C API ?

You mean something like:
PyObject_CallFunctionObjArgs(
                          (PyObject*)&PyType_Type,
                          classname, classbases, classdict,
                          NULL);
?

There may be faster ways, but I generally start writing extensions by
keeping the C code as "close" to equivalent Python code as I can, which
means lots of use of PyObject_... level functions.  If and when I need
to squeeze out the last few cycles, I may dig into those for
optimization purposes, but what was that quip about premature
optimization, again....?-)


Alex



More information about the Python-list mailing list