Power to Build

Home » Misc » Gotcha: Python fuction/method calls

Gotcha: Python fuction/method calls

I am taking a Python course at Coursera.org. While doing one of the homeworks, I was able to get a successful connection to the database and using a cursor loaded the data into a table in SQLite. It looked like it was loading fine. But, SQLite Browser did not show anything in the table.

Typically, this could mean, you forgot to commit the transaction. There I have conn.commit. So that’s not it. Or is it?

I started printing debug messages to see before and after. SELECT from the table being inserted. I even tried wrapping the code inside try..except block. No errors!! All pointed to the rows being in the table. But it only showed up inside but outside the program.

Again, from my experience with other databases, it pointed to COMMIT not being there. Is SQLite broken?? I moved around my commit statement to no avail. Then I looked at the commit statement itself. I had:

conn.commit

Wait! Is there a problem with my commit statement? In SQLite, the SQL operations are done through API functions. So, isn’t commit a method in Connection object? Should I include ()? I did that and the transaction did COMMIT!!!!

conn.commit()

Some other languages make it optional (I think, Perl for e.g., accepts function calls without parantheses). I got spoiled by that. Not so with Python! This is what I call the culture of a particular language. Python silently ignored the identifier commit and the program behavior is totally unpredictable.

The lesson is don’t miss your parantheses in a function/method call!


Comments, please?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: