[Python-Dev] PEP 442: Safe object finalization

Richard Oudkerk shibturn at gmail.com
Sat May 18 16:52:56 CEST 2013

On 18/05/2013 3:18pm, Antoine Pitrou wrote:
> It works fine:
> $ ./python sbt.py
> <__main__.Node object at 0x7f3acbf8f400> <__main__.Node object at 0x7f3acbf8f878>
> <__main__.Node object at 0x7f3acbf8f878> <__main__.Node object at 0x7f3acbf8f400>
> The reason is that, when you execute "del self.next", this removes the
> last reference to self.next and destroys it immediately.

So even more contrived:

      class Node:
          def __init__(self, x):
              self.x = x
              self.next = None
          def __del__(self):
              print(self.x, self.next.x)
              del self.x

      a = Node(1)
      b = Node(2)
      a.next = b
      b.next = a
      del a, b


More information about the Python-Dev mailing list