PEP-442 - Python 3.4 and finalizations (__del__)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Sep 14 05:42:33 EDT 2013


On Sat, 14 Sep 2013 10:14:27 +0200, Marco Buttu wrote:

> Hi all. Will the following code in Python 3.4 print "Goodbye from B()"
> and "Goodbye from A():

Perhaps you should try it and find out.


[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
>>>



Does that answer your question?



-- 
Steven



More information about the Python-list mailing list