vendredi 30 janvier 2015

Python: How can I create a "global object"?

I have a problem with the following code. As you can see I set up an sqlite3 database in the function setup_session(), though when I try to run it it throws an exception because the object cursor which I created in setup_session() is only available inside the function.



import sqlite3

def setup_session():
db = sqlite3.connect("data.db")
cursor = db.cursor()


setup_session()

cursor.execute("CREATE TABLE subjects (subject text)")


How can I change it so that cursor is also available from outside the function?


Aucun commentaire:

Enregistrer un commentaire