Embedding Python

  • Thread starter =?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=
  • Start date
?

=?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=

Hi,

Imagine that you have a PyObject pointer 'object'
pointing to a Python integer ... let's say 42.

How would do you attach the variable "answer" to
it so that the code

PyRun_SimpleString("print answer");

works as expected ?

My current solution is:

__main__ = PyImport_ImportModule("__main__");
PyObject_SetAttrString(__main__, "answer", object);

Anything better ?

SB
 
S

Stefan Seefeld

Sébastien Boisgérault said:
Hi,

Imagine that you have a PyObject pointer 'object'
pointing to a Python integer ... let's say 42.

How would do you attach the variable "answer" to
it so that the code

PyRun_SimpleString("print answer");

works as expected ?

My current solution is:

__main__ = PyImport_ImportModule("__main__");
PyObject_SetAttrString(__main__, "answer", object);

Anything better ?

You could use PyRun_String instead, which lets you
pass global and local dictionaries as context.
That may provide more fine-grained control, depending
on what you want.

Regards,
Stefan
 

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,252
Messages
2,571,267
Members
47,908
Latest member
MagdalenaR

Latest Threads

Top