[C++-sig] Re: C++ Python Scope Problems

David Abrahams dave at boost-consulting.com
Wed Nov 24 14:39:55 CET 2004


Chris Targett <xin at xlevus.net> writes:

> The Code:
> http://orcon.xlevus.net/PyTest.tar.gz
> I have included CMake Files and Make Makefiles. Requires boost and python
> libraries to compile.
>
> My Problem.
> I have my Test C++ module (called 'Test' and exposed it through boost.)
> In main.py I have created a pure python equlavient to my `Test` module called
> pyTest.
> In testScript.py I have a little function (testFunction) that Prints the three
> varibles (of the object thats passed to it), increments the values by one, then
> prints them again.
>
> Back to main.py. Now when the functions are called to the C++ module, the values
> are changed by `testFunction` while inside the scope of the function, but when
> returned back to the default scope (what im assuming its called) the Values get
> reverted back to their original.
>
> When the same stuff is done on the Pure Python equlaivient, the values remain
> changed when reverted back to the default scope.
>
> What I want is, my C++ module to be more like my Python Module, and have the
> varibles changed by the function.
>
> Uh, I hope i make sense. 

I haven't looked at your archive, but I'm guessing that your problem
is unfixable.  In Python variables don't have their values changed,
they are rebound.  Try:

     python -c "x = 1; print(id(x)); x += 1; print(id(x))"

HTH,
-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com




More information about the Cplusplus-sig mailing list