scope question

Skip Montanaro skip at
Thu Nov 15 16:14:57 CET 2001

    glenn> To close out the cursor and the database connection the cursor is
    glenn> passed to the close method.  How do I get this to work?  I find
    glenn> that I cannot simply do this:

    glenn> def close(cursor):
    glenn>         cursor.close()
    glenn>         db.close()

    glenn> This fails with a NameError: global name 'db' is not defined.

That stands to reason.  Unless the name db is in the module scope of the
close function, you won't find it.  Why not just pass the db object into the
close method and simply delete the cursor object?  I normally use many
cursors per db connection.  It's not obvious that you'd want to delete them
at the same time anyway.

I've never closed a cursor object explicitly.  Failing to close it doesn't
create a memory leak, with or without gc enabled.

Skip Montanaro (skip at -
It's only a 25% solution to our problems.  Of course, we only want to solve
25% of our problems, so it becomes a 100% solution.

More information about the Python-list mailing list