[DB-SIG] "lists of tuples" vs. "tuples of tuples"
mal at egenix.com
Tue Feb 3 22:12:17 CET 2015
On 03.02.2015 21:30, Michael Bayer wrote:
>> [By the way, both Rows and Row objects are made immutable, so that an unwary programmer does not receive an unwelcome surprise by attempting to alter data without using an SQL statement.]
> I would actually favor the result list to be an immutable one as well, and the ideal structure would be an immutable list (which is of course entirely straightforward in Python). It’s unusual that Marc suggests the collection is explicitly mutable, and that this is a common use case.
I was probably unclear in what I wrote. I wasn't emphasizing on the
mutability of the list elements, but rather the subsequent use of
the result set list in the applications, which often slice and dice
the list in various ways needed by the use case.
A typical use case for this is fetching result sets in chunks
and then recombining them in a larger list for further processing.
> Overall though, I don’t care much that the collection is mutable or not though in this area as well, a clearer specification would only serve the purpose to eliminate this decision / discussion needing to happen at the endpoints of every DBAPI driver. A spec that provides clear guidance rather than loose suggestions would save a lot of effort. Such a spec is of course more difficult to produce, but IMHO would be worth it.
The DB-API does provide guidance, but doesn't mandate specific
What we can do is add a note to recommend that implementations
return results sets as list of tuples, unless special result set and
row objects are more appropriate or needed.
BTW: I still don't quite understand the motivation to have this
pinned down in the spec. If an application needs a list, the DB-API
mandates the sequence protocol, so running:
rs = list(cursor.fetchall())
will always give you a list as result set.
Note that tuples are faster than lists in Python, so a performance
aware module author might want to use this fact to squeeze out a
few more cycles.
People who have purely numeric needs, can also use numpy arrays as
containers (even to represent the complete result set),
since they also implement the sequence protocol.
Professional Python Services directly from the Source (#1, Feb 03 2015)
>>> Python Projects, Coaching and Consulting ... http://www.egenix.com/
>>> mxODBC Plone/Zope Database Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
More information about the DB-SIG