Nested try...except

Duncan Booth duncan.booth at invalid.invalid
Thu Apr 3 10:36:55 EDT 2008


Carl Banks <pavlovevidence at gmail.com> wrote:

> Perhaps the advent of with blocks will help reduce this error in the
> future.

Indeed, and to encourage its use I think this thread ought to include the 
'with statement' form of the function:

from __future__ import with_statement
from contextlib import closing

def count(self):
    with closing(sqlite.connect(
        self.filename,isolation_level=ISOLATION_LEVEL)) as db:
        with closing(db.cursor()) as cur:
            cur.execute("select count(*) from sessions")
            return cur.fetchone()[0]



More information about the Python-list mailing list