[DB-SIG] "lists of tuples" vs. "tuples of tuples"

Michael Bayer mike_mp at zzzcomputing.com
Wed Feb 4 00:10:34 CET 2015



M.-A. Lemburg <mal at egenix.com> wrote:

> 
> The DB-API does provide guidance, but doesn't mandate specific
> implementations.
> 
> 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:

Marc -

It’s mostly an issue of semantic correctness. A tuple implies a certain kind
of role for a data structure. From a “Does it work?” perspective, there’s
really no problem at all, except if we’re concerned about applications that
assume the return of fetchall() is mutable. Python is in often enthusiastic
about this kind of thing in general, from pep8 to naming conventions to
everything else (to pointing out in the Python docs what the usual
role of tuples is). But there’s no pressing issue here other than that.



> 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.
> 
> -- 
> Marc-Andre Lemburg
> eGenix.com
> 
> 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
>               http://www.egenix.com/company/contact/


More information about the DB-SIG mailing list