Jeff Schwab <[EMAIL PROTECTED]> writes: > The most traditional, easiest way to open a file in C++ is to use an > fstream object, so the file is guaranteed to be closed when the > fstream goes out of scope.
Python has this too, except it's using a special type of scope created by the "with" statement. > CPython offers a similar feature, since > you can create a temporary object whose reference count will become > zero at the end of the statement where it is defined: > $ echo world >hello > $ python > >>> file('hello').read() > 'world\n' CPython does not guarantee that the reference count will become zero at the end of the statement. It only happens to work that way in your example, because the file.read operation doesn't make any new references to the file object anywhere. Other code might well do something different, especially in a complex multi-statement scope. Your scheme's determinism relies on the programmer accurately keeping track of reference counts in their head, which is precisely what automatic resource management is supposed to avoid. If you want reliable destruction it's better to set it up explicitly, using "with". -- http://mail.python.org/mailman/listinfo/python-list