embedding python and new-style classes

M

mike

Hi All,

I have some "new-style" classes written in Python that I would like to embed
inside a C++ application (somewhat grudgingly - if I had my way, the whole
thing would be written in Python). Anyway, I've done this once before, but
that was with "classic" Python classes. In that case, I used
PyDict_GetItemString to get a reference to the class I wanted and then used
PyInstance_New() to create the instances.

With the new-style class, the thing I get back from PyDict_GetItemString
appears to be a type reference (rather than a class). So instead of using
PyInstance_New(), I tried using PyType_GenericNew() (the API seemed
analogous). However, it doesn't look like the __new__ or __init__ methods
of my underlying Python object are being invoked. I don't have a lot of
experience with this, so I'm not sure if I've simply made a mistake or
whether I'm going about this all wrong. If someone could tell me the proper
API calls for instantiating a new-style class, or point me to an example,
I'd really appreciate it.

Thanks in advance,
mike
 
S

Stephen Horne

If someone could tell me the proper
API calls for instantiating a new-style class, or point me to an example,
I'd really appreciate it.

I'm relatively new at extending/embedding so I don't know if this has
changed, but doing this recently I simply noted that Python class
objects are callables and that calling them returns an instantiation.
I use Boost.Python, but I imagine you can simply use
PyObject_CallMethod with the class object.
 
M

mike

I'm relatively new at extending/embedding so I don't know if this has
changed, but doing this recently I simply noted that Python class
objects are callables and that calling them returns an instantiation.
I use Boost.Python, but I imagine you can simply use
PyObject_CallMethod with the class object.

Good point - I just tried using PyObject_Call(), and that seems to work.
Thanks for the help!

mike
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top