[C++-sig] Inconsistency with Boost.Python code
seefeld at sympatico.ca
Thu Oct 9 19:06:50 CEST 2008
I believe the reason for this behavior is that an implicit cast from
object to dict and list will cause a copy, so you don't actually insert
the path into the original (global) path object, but a local copy, which
obviously isn't seen by the rest of the application.
> Suppose the following C++ Boost.Python code below. Note that I am
> embedding the python interpreter to execute python scripts from C++:
> using namespace boost::python;
> object imported( import( "sys" ) );
> dict sysdict( imported.attr( "__dict__" ) );
The call to 'attr()' returns an object, and you assign it to a dict.
This causes a copy. Try this instead:
object dict_object = imported.attr("__dict__");
dict sysdict = extract<dict>(dict_object);
> list syspath( sysdict["path"] );
And likewise here:
object list_object = sysdict["path"];
list syspath = extract<list>(list_object);
> syspath.append( "C:\testing" );
However, as you have discovered, there are ways to fold these lines to
make it more compact. I only spelled the individual steps out to
illustrate what's going on underneath.
...ich hab' noch einen Koffer in Berlin...
More information about the Cplusplus-sig