PEP-442 - Python 3.4 and finalizations (__del__)

Marco Buttu marco.buttu at gmail.com
Sat Sep 14 06:10:57 EDT 2013


On 09/14/2013 11:42 AM, Steven D'Aprano wrote:


> Perhaps you should try it and find out.

Hi Steven, thanks for your answer. I tried it with Python 3.4a2, but I 
did not see any output from __del__().

> [steve at ando ~]$ python3.4 -E
> Python 3.4.0a1+ (default:becbb65074e1, Aug 26 2013, 03:57:58)
> [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> >>>
>>>> >>>class A:
> ....      def __init__(self, a):
> ....          self.a = a
> ....          print('In A.__init__()')
> ....      def __del__(self):
> ....          print('Goodbye from A()')
> ....
>>>> >>>class B:
> ....      def __init__(self):
> ....          self.b = A(self) # Reference cycle
> ....          print('In B.__init__()')
> ....      def __del__(self):
> ....          print('Goodbye from B()')
> ....
>>>> >>>b = B()
> In A.__init__()
> In B.__init__()
>>>> >>>del b
>>>> >>>
>>>> >>>
>>>> >>>import gc
>>>> >>>gc.collect()
> Goodbye from B()
> Goodbye from A()
> 4

I thought in this case, the __del__() methods should have called 
automatically...

> Does that answer your question?

Yes, thanks a lot Steven, I saw the gc.collect() does not call the 
__del__() method in Python 3.3, so I think I will look at the 
documentation of gc in order to understand better. Thanks again, Marco


-- 
Marco Buttu



More information about the Python-list mailing list