Loading select queries into objects
graham at coms.com
Mon Jun 18 14:37:50 CEST 2001
Hi. I've just started playing with database access from Python and have
stumbled across yet another hole in my knowledge of Python idioms.
I've got a class that has an attribute for each column in a database
table, and an accessor method that sets the value of each attribute. I
then populate the attributes with the result of a database query.
Here's an example:
query = """
SELECT column1, column2, some_column
cursor = self.db.cursor()
tup = cursor.fetchone()
self.column1(tup) # this could be better!
What I'm wondering is; is there an idiomatic way of applying the values
in a tuple to a list of functions? In Perl I'd use map(), but I'm not
sure if there's a "cleaner" way in Python.
When the number of columns grows large the above is not only ugly, but a
nightmare to maintain.
More information about the Python-list