Loading select queries into objects

Graham Ashton graham at coms.com
Mon Jun 18 08:37:50 EDT 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
          FROM a_table
    """

    cursor = self.db.cursor()
    cursor.execute(query)
    tup = cursor.fetchone()

    self.column1(tup[0])		# this could be better!
    self.column2(tup[1])
    self.some_column(tup[2])

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.

Thanks.



More information about the Python-list mailing list