On Apr 2, 9:06 am, [EMAIL PROTECTED] wrote:
> Hi,
>
> I found the following code on the net -
>
> http://mail-archives.apache.org/mod_mbox/httpd-python-cvs/200509.mbox/[EMAIL
> PROTECTED]
>
> def count(self):
> - db = sqlite.connect(self.filename,
> isolation_level=ISOLATION_LEVEL)
> - try:
> - try:
> - cur = db.cursor()
> - cur.execute("select count(*) from sessions")
> - return cur.fetchone()[0]
> - finally:
> - cur.close()
> - finally:
> - db.close()
>
> I don't understand though why the second try is not after the line cur
> = db.cursor(). Can anyone explain for me why?
It's a pretty common mistake to make, I assume because there's a
tendency to line up the init and finalize statements. In other words,
it looks wrong for open and close to be in different columns:
open()
try:
do_stuff()
finally:
close()
It's almost always wrong for initiazation to be inside of the try
block.
Perhaps the advent of with blocks will help reduce this error in the
future.
Carl Banks
--
http://mail.python.org/mailman/listinfo/python-list