[C++-sig] Pointer to existing Python object

Andreas Beyer beyer at imb-jena.de
Fri May 20 04:11:42 CEST 2005


David Abrahams wrote:

>David Abrahams <dave at boost-consulting.com> writes:
>
>  
>
>>Andreas Beyer <beyer at imb-jena.de> writes:
>>
>>    
>>
>>>David, thanks a lot for the help! However, I think your suggestion does 
>>>not yet solve the problem. See below.
>>>      
>>>
>>It should; I have a fix in the works.  I'm preparing to check it into
>>CVS.
>>    
>>
>
>Done.  Please let me know if it works for you.
>
>  
>
It does. It works with the code at the end of my last posting:
http://mail.python.org/pipermail/c++-sig/2005-May/008910.html

Now I can do this:
 >>> from ptr_test import *
 >>> a=A()
 >>> b=B()
 >>> b.set(a)
 >>> a
<ptr_test.A object at 0x4029f89c>
 >>> b.get()
<ptr_test.A object at 0x4029f89c>
 >>> b=B()
 >>> a=b.get() # make new A inside b (factory method)
 >>> a2=b.get() # test if the two python wrappers are the same
 >>> a
<ptr_test.A object at 0x4029bb54>
 >>> a2
<ptr_test.A object at 0x4029bb54>
 >>> a.foo=42 # alter object in python
 >>> a2.foo # container b 'remembers' the change
42
 >>>

I think this is a very strong concept of general interest: I can now 
change Python attributs of wrappers (such as adding attributs in 
Python), although the objects were created and are managed by C++ code. 
(By management I mean live-time management.) There is no difficulty if 
you get an object from a C++ factory class and store it in a Python 
container. However, I want to store it in a C++ container. In general, 
the two classes (the container and the factory) can of course be 
distinct. I don't think David's sollution is affected by this.

Thanks! Andreas




More information about the Cplusplus-sig mailing list