"Then you can create a file, write to it, and close it, all in one line. The closing is implicit, of course; it happens right away as soon as there are no more references to the file. Because, obviously, nothing else would make sense."
This behavior isn't even guaranteed in CPython (and never has been as far as I know). There's a problem with deterministic destructors and refcounting: if there's a reference cycle, which destructor gets called first?
import weakref
import gc
class Something(object):
def __init__(self, other):
self.other = other
def __del__(self):
print '__del__ called!'
s1 = Something(None)
s2 = Something(s1)
s1.other = s2
del s1
del s2
gc.collect()
print gc.garbage
Even the cyclic garbage collector won't pick this up. These kinds of issues come up more and more if you rely on python's __del__ methods (aka destructors). The solution? Quit complaining and just use a with block. It isn't that bad.
It's not just about reference counting though. It's a weakness of deterministic destructors. When you have a reference cycle, it's impossible to call destructors deterministically even if you aren't using reference counting.
This behavior isn't even guaranteed in CPython (and never has been as far as I know). There's a problem with deterministic destructors and refcounting: if there's a reference cycle, which destructor gets called first?
Even the cyclic garbage collector won't pick this up. These kinds of issues come up more and more if you rely on python's __del__ methods (aka destructors). The solution? Quit complaining and just use a with block. It isn't that bad.