On 10 February 2012 20:16, Dirkjan Ochtman <dirkjan@ochtman.nl> wrote:
There are some simple patterns that are great with refcounting and not
so great with garbage collection. We encountered some of these with
Mercurial. IIRC, the basic example is just

open('foo').read()

With refcounting, the file will be closed soon. With garbage
collection, it won't. Being able to rely on cleanup per frame/function
call is pretty useful.

This is the #1 anti-pattern that shouldn't be encouraged. Using this idiom is just going to cause problems (mysterious exceptions while trying to open files due to running out of file handles for the process) for anyone trying to port your code to other implementations of Python.

If you read PEP 343 (and the various discussions around that time) it's clear that the above anti-pattern is one of the major driving forces for the introduction of the 'with' statement.

Tim Delaney