[Python-ideas] namedtuple is not as good as it should be

Christian Tismer tismer at stackless.com
Mon Jun 10 09:45:35 CEST 2013


On 10.06.13 08:47, Andrew Barnert wrote:
> On Jun 9, 2013, at 23:28, Christian Tismer <tismer at stackless.com> wrote:
>
>> But when you continue and create tables, which are typically not having
>> a static class definition and you come from the dynamic of tuples,
>> the problem suddenly becomes very visible.
>>
>> The whole issue is that a dynamically usable structure like tuple suddenly
>> becomes statically defined, and you need to cope with that.
> If your structure is dynamic, shouldn't you be using mapping syntax rather than attribute syntax anyway? After all, you can't write row.last_name unless you know last_name statically, and you wouldn't want to write getattr(row, namefield) in place of row[namefield].
>
> In other words, I think you just want OrderedDict here, or maybe something like sqlite3.Row.

Sure there are alternatives.
Surely not OrderedDict.

I have hundreds of columns in a table with a billion rows.
This is what still fits into memory for a very fast readonly database
that I generate instead of using a real DB monster.

namedtuple is just a convenience that I used to play interactively
with some records, and it turned out to be a showstopper in the end.

> Also, tuples themselves are normally used for static structures—you know you're getting back 4 values, and tup[1] is the last name, etc. Of course you _can_ use them just as "frozen lists", but that's not the paradigm case that we should be looking to extend to new types.

I am talking of rows as tuples. That is the right way to use tuples.
> I realize that if you're building an ORM library, or a bridge library like appscript or win32com, you need to create things dynamically that will be used as static objects by the client code, even though you don't know the structure inside the library until runtime. But I think that's a special case.

I don't need advice how to build a database. I want the namedtuple
to improve, and be able to switch their use on and off without having
to think.
That was my problem:
I wanted to try alternative implementations, and there namedtuple
helps very much at first sight but then creates a problem that you
don't expect.

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at stackless.com>
Software Consulting          :     Have a break! Take a ride on Python's
Karl-Liebknecht-Str. 121     :    *Starship* http://starship.python.net/
14482 Potsdam                :     PGP key -> http://pgp.uni-mainz.de
phone +49 173 24 18 776  fax +49 (30) 700143-0023
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
       whom do you want to sponsor today?   http://www.stackless.com/



More information about the Python-ideas mailing list