3 ms·
It won’t be released immediately, that’s the point. It will be “released at some point in the future and definitely not during interpreter shutdown”. A “with”
by orf 6y ago
It won’t be released immediately, that’s the point. It will be “released at some point in the future and definitely not during interpreter shutdown”.
A “with” statement ensures the release happens deterministically, which it turns out is a desirable trait to have.
- superbatfish 6y agoThe Python standard makes no guarantee, but in CPython, the example above is deterministic. The cycle-detecting garbage collector is not involved in this case. Just refcounting.
- james412 6y agoIf open() throws an exception, perhaps after the FD is already open (Python does non-trivial IO to a file immediately after open), or read() throws an exception, the file object is at the mercy of any try/except higher in the stack, any bugs/"features" contained in those, and any sys.excepthook installed like Sentry The example above is deterministic only on current versions of CPython and only if it is guaranteed to succeed and if it is the only line of code in the program now and in perpetuity.