Embedding Python

Stefan Seefeld seefeld at sympatico.ca
Fri Jun 17 16:36:07 EDT 2005


Sébastien Boisgérault wrote:
> 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



More information about the Python-list mailing list