extending: new type instance

Michael Hudson mwh at python.net
Wed Aug 17 17:57:50 CEST 2005

"BranoZ" <zarnovican at gmail.com> writes:

> I'm writing my own (list-like) type in C. It is implementing
> a Sequence Protocol. In 'sq_slice' method I would like to return
> a new instance of my class/type.
> How do I create (and initialize) an instance of a given
> PyTypeObject MyType ?
> I have tried to provide (PyObject *)&MyType as 'class' argument to:
> PyInstance_New(PyObject *class, PyObject *arg, PyObject *kw)
> It failed the PyClass_Check.
> I have also found a couple of PyObject_New functions which accept
> PyTypeObject as an argument. They look very low-level and obviously
> don't call __init__. Should I do it myself manualy ?

PyObject_New is the usual way, although there are others --
MyType.tp_new, PyObject_Call ...


81. In computing, turning the obvious into the useful is a living
    definition of the word "frustration".
  -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html

More information about the Python-list mailing list