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

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Thu Nov 25 00:57:54 CET 2004


--- Chris Targett <xin at xlevus.net> wrote:

> I see. But if i do:
> 
> 	import libHg 	# name of C++ library
> 	foo = libHg.Test()
> 	print id(foo.var1), foo.var1
> 	foo.var1 += 1
> 	print id(foo.var1), foo.var1
> 
> I get
> 	135523772 0
> 	135523760 1
> 
> Which clearley shows the varible is 'rebound'

I got me worried..., but what you show above works correctly as one would
expect. However, it has not much to do with your original problem. When you
don't get the expected result you are doing something far more complex:

        PyObject *pFunc = getPyFunc("testScript","testFunction");
        call<void>(pFunc, this);

David, please correct me if I am wrong: I think the call<>() must create a
temporary Python object with a copy of "this". The temporary disappears at the
end of the function call.

David, is there a way to create a temporary with a pointer to "this", e.g.
call<void>(pFunc, *this);
?

To the OP: as in real life, there are good and bad neighborhoods. In my
judgement you are in a bad neighborhood. If you call Python from C++ I am
wondering why you are in C++ in the first place. There will not be much of a
speed gain compared to a pure Python solution.
Here is a simple rule that works very well for me:

  Never call Python from C++.

This side-steps a lot of potential surprises like your original problem.
In addition, I'd bet your code will be better organized.

Cheers,
Ralf



	
		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - You care about security. So do we. 
http://promotions.yahoo.com/new_mail



More information about the Cplusplus-sig mailing list