469,304 Members | 2,018 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

extending: new type instance

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

Aug 16 '05 #1
2 1639
"BranoZ" <za********@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 ...

Cheers,
mwh

--
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
Aug 17 '05 #2
Michael Hudson wrote:
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

Aug 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by NadaHombre | last post: by
5 posts views Thread by Casper B | last post: by
3 posts views Thread by Lars Moastuen | last post: by
5 posts views Thread by vbgunz | last post: by
13 posts views Thread by eman1000 | last post: by
9 posts views Thread by Robert | last post: by
4 posts views Thread by Ian Richardson | last post: by
3 posts views Thread by katis | last post: by
7 posts views Thread by Maximus Decimus | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.