Searching equivalent to C++ RAII or deterministic destructors

Peter Otten __peter__ at web.de
Thu Jul 2 08:54:50 EDT 2009


Dave Angel wrote:

> But I'm guessing you want something that automatically deletes objects
> whenever the last reference disappears.  That's an implementation
> detail, not a language guarantee.  In particular CPython does what you
> want, by using reference counting.  That's the only Python I've used, so
> it's only hearsay when I say that other implementations, (maybe Cython
> or Jython) do not all work the same way.

Here are some examples from Kubuntu 9.04's zoo of python implementations:

$ cat del.py
import sys
print sys.version

class A(object):
    def __init__(self, x):
        self.x = x
    def __del__(self):
        print "releasing A(%r)" % self.x

def f():
    a = A("local in function")
f()

a = A("global (one ref)")

c = A("global (cycle)")
c.a = c
del c

b = A("global (no refs)")
del b

print "about to quit"

$ python del.py
2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3]
releasing A('local in function')
releasing A('global (no refs)')
about to quit
releasing A('global (one ref)')

$ jython del.py
2.2.1
about to quit

$ ipy del.py
2.4.0 (IronPython 1.1.1 (1.1.1) on .NET 2.0.50727.42)
about to quit
releasing A('global (no refs)')
releasing A('global (cycle)')
releasing A('local in function')

Unhandled Exception: System.ArgumentException: I/O operation on closed file
  at IronPython.Runtime.PythonFile.ThrowIfClosed () [0x00000]
  at IronPython.Runtime.PythonFile.Write (System.String s) [0x00000]
$

IronPython sometimes segfaulted.

Peter




More information about the Python-list mailing list