extending: new type instance

B

BranoZ

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 ?

BranoZ
 
M

Michael Hudson

BranoZ said:
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 ...

Cheers,
mwh
 
B

BranoZ

Michael said:
PyObject_New is the usual way, although there are others --

Thanks for an answer !
MyType.tp_new, PyObject_Call ...

Does this mean, that I can call my newly defined
MyType_new, MyType_init directly ??

I was afraid that Python might do some pretty important
housekeeping stuff around them.

BranoZ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,312
Members
47,988
Latest member
HarryBeck

Latest Threads

Top