[C++-sig] C++ chain method works unexpectedly in Python
I have a python module written in C++, and the C++ code has a few instances that involve method chaining. I was experimenting with python support for these methods and I found something odd. However it seems this is more a python eccentricity than Boost Python, consider the following code sample. class Test(): def __init__( self ): print "Init" def __del__( self ): print "Del" def foo( self ): print "Foo" return self def bar( self ): print "Bar" return self And some sample uses: >> Test().foo().bar() Init Foo Bar <__main__.Test instance at 0x2aef40e33998> >> Note that __del__ was never called, the object still exists somewhere.. There is another odd thing though, if I repeat the same line, the previous version gets destroyed and replaced by the new version, but only after the chain methods are done, eg: >>> Test().foo().bar() Init Foo Bar Del <-- Old one, NOT the current instance <__main__.Test instance at 0x2b72bc78d998> >> However if I do: def Tester(): Test().foo().bar() # To make sure del is not called by the function returning while True: pass >> Tester() Init Foo Bar Del Traceback (most recent call last): File "", line 1, in File "", line 4, in Tester KeyboardInterrupt >> That time __del__ was called immediately ( as intended ). Is there something I should be aware of here? Some sort of difference between the global instances and ones defined in a function? Can I do anything so global instances are cleaned up immediately like they are inside functions? Thanks ___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] C++ chain method works unexpectedly in Python
On 2011-04-17 20:56, Charles Solar wrote: Is there something I should be aware of here? Just that Python uses garbage collection and you mustn't rely on your objects being destroyed at a particular point in the program flow. :-) Some sort of difference between the global instances and ones defined in a function? Can I do anything so global instances are cleaned up immediately like they are inside functions? No idea, sorry. Stefan -- ...ich hab' noch einen Koffer in Berlin... ___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] C++ chain method works unexpectedly in Python
Well I know python uses ref counting, so I was hoping the support for chain methods would be near identical to C++'s since python would not have any reference to hold onto for Test(). It works great when inside a function, but not so well in the global namespace. :( On Sun, Apr 17, 2011 at 8:06 PM, Stefan Seefeld wrote: > On 2011-04-17 20:56, Charles Solar wrote: > >> >> Is there something I should be aware of here? >> > > Just that Python uses garbage collection and you mustn't rely on your > objects being destroyed at a particular point in the program flow. :-) > > > Some sort of difference between the global instances and ones defined in >> a function? Can I do anything so global instances are cleaned up >> immediately like they are inside functions? >> > > No idea, sorry. > >Stefan > > > -- > > ...ich hab' noch einen Koffer in Berlin... > > ___ > Cplusplus-sig mailing list > Cplusplus-sig@python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > ___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] C++ chain method works unexpectedly in Python
Python Interactive shell stores result of an expression to a variable named '__builtin__._'. So, ``del _`` may help you. This behavior is only on interactive shell. When running script, '_' is not used and Python may acts you expect. On Mon, Apr 18, 2011 at 9:56 AM, Charles Solar wrote: > I have a python module written in C++, and the C++ code has a few instances > that involve method chaining. I was experimenting with python support for > these methods and I found something odd. > However it seems this is more a python eccentricity than Boost Python, > consider the following code sample. > > class Test(): > def __init__( self ): > print "Init" > def __del__( self ): > print "Del" > def foo( self ): > print "Foo" > return self > def bar( self ): > print "Bar" > return self > > And some sample uses: > >>> Test().foo().bar() > Init > Foo > Bar > <__main__.Test instance at 0x2aef40e33998> >>> > > Note that __del__ was never called, the object still exists somewhere.. > There is another odd thing though, if I repeat the same line, the previous > version gets destroyed and replaced by the new version, but only after the > chain methods are done, eg: > Test().foo().bar() > Init > Foo > Bar > Del <-- Old one, NOT the current instance > <__main__.Test instance at 0x2b72bc78d998> >>> > > However if I do: > > def Tester(): > Test().foo().bar() > # To make sure del is not called by the function returning > while True: > pass > >>> Tester() > Init > Foo > Bar > Del > Traceback (most recent call last): > File "", line 1, in > File "", line 4, in Tester > KeyboardInterrupt >>> > > That time __del__ was called immediately ( as intended ). > > Is there something I should be aware of here? Some sort of difference > between the global instances and ones defined in a function? Can I do > anything so global instances are cleaned up immediately like they are inside > functions? > > Thanks > > > > ___ > Cplusplus-sig mailing list > Cplusplus-sig@python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- INADA Naoki ___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] C++ chain method works unexpectedly in Python
Ahh very cool, exactly what I was looking for. I did not know about __builtin__._ Figured it was something funny like that, glad its just a shell thing. Thank you On Sun, Apr 17, 2011 at 9:07 PM, INADA Naoki wrote: > Python Interactive shell stores result of an expression to a variable > named '__builtin__._'. > So, ``del _`` may help you. > > This behavior is only on interactive shell. When running script, '_' > is not used and Python > may acts you expect. > > > On Mon, Apr 18, 2011 at 9:56 AM, Charles Solar > wrote: > > I have a python module written in C++, and the C++ code has a few > instances > > that involve method chaining. I was experimenting with python support > for > > these methods and I found something odd. > > However it seems this is more a python eccentricity than Boost Python, > > consider the following code sample. > > > > class Test(): > > def __init__( self ): > > print "Init" > > def __del__( self ): > > print "Del" > > def foo( self ): > > print "Foo" > > return self > > def bar( self ): > > print "Bar" > > return self > > > > And some sample uses: > > > >>> Test().foo().bar() > > Init > > Foo > > Bar > > <__main__.Test instance at 0x2aef40e33998> > >>> > > > > Note that __del__ was never called, the object still exists somewhere.. > > There is another odd thing though, if I repeat the same line, the > previous > > version gets destroyed and replaced by the new version, but only after > the > > chain methods are done, eg: > > > Test().foo().bar() > > Init > > Foo > > Bar > > Del <-- Old one, NOT the current instance > > <__main__.Test instance at 0x2b72bc78d998> > >>> > > > > However if I do: > > > > def Tester(): > > Test().foo().bar() > > # To make sure del is not called by the function returning > > while True: > > pass > > > >>> Tester() > > Init > > Foo > > Bar > > Del > > Traceback (most recent call last): > > File "", line 1, in > > File "", line 4, in Tester > > KeyboardInterrupt > >>> > > > > That time __del__ was called immediately ( as intended ). > > > > Is there something I should be aware of here? Some sort of difference > > between the global instances and ones defined in a function? Can I do > > anything so global instances are cleaned up immediately like they are > inside > > functions? > > > > Thanks > > > > > > > > ___ > > Cplusplus-sig mailing list > > Cplusplus-sig@python.org > > http://mail.python.org/mailman/listinfo/cplusplus-sig > > > > > > -- > INADA Naoki > ___ > Cplusplus-sig mailing list > Cplusplus-sig@python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > ___ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig