Mario Figueiredo wrote: > Currently i'm using the following code to transform a row fetched from an > sqlite database into a dictionary property:
class Unknown: > def __init__(self, id_): > self.id = id_ > self.data = None > ... > conn = sqlite3.connect('data') > conn.row_factory = sqlite3.Row > row = conn.execute(query, {'id': str(id_)}).fetchone() > conn.close() > > if row: > self.data = dict(zip(row.keys(), tuple(row))) > > I have two questions: > > 1. Is this an acceptable idiom for the construction of self.data > dictionary, > or do I have a better and more readable option? If sqlite3.Row is not dict-like enough to use it directly self.data = row you can either convert the Row object with self.data = dict(row) or use the recipe for a row factory provided in the documentation at https://docs.python.org/dev/library/sqlite3.html#sqlite3.Connection.row_factory that constructs a dict directly. And now an unsolicited remark: if you have more than one instance of Unknown you might read the data outside the initialiser or at least keep the connection open and pass a connection or cursor object to the initialiser. -- https://mail.python.org/mailman/listinfo/python-list