Ulrich Eckhardt wrote: > Bearophile wrote: >> Ulrich Eckhardt: >>> a way to automatically release the resource, something >>> which I would do in the destructor in C++. >> >> Is this helpful? >> http://effbot.org/pyref/with.htm > > Yes, it aims in the same direction. However, I'm not sure this applies to > my case. The point is that the resource handle is not just used locally in > a restricted scope but it is allocated and stored. The 'with' is something > that makes sense in the context of mutex locking, where you have a > well-defined critical section.
Isn't that exactly what RAII does? > What I need is something similar to open(), > which returs a file. When the last reference to that object goes out of > scope, the underlying file object is closed. You can go ahead and implement a __del__() method. It will often work in CPython, but you get no guarantees, especially when you have reference cycles and with other Python implementations that don't use refcounting. Peter -- http://mail.python.org/mailman/listinfo/python-list