f---ing typechecking
James Stroud
jstroud at mbi.ucla.edu
Thu Feb 15 07:38:41 EST 2007
Paul McGuire wrote:
> Since tuples are immutable, I think of them as fixed data objects with
> some simple sequential structure, as opposed to lists which are much
> more dynamically accessible/updateable data containers. Back in my
> relational database design days, I sometimes had to create a primary
> key for a table by combining values stored in two or more columns -
> neither column value alone was unique, but the combination of them
> was, and so made a good retrieval index. In Python, such data pairs
> would be ideally represented with tuples, in support of in-memory data
> cacheing or tree indexing - for a given record, the values don't
> change, so the immutability of their tupleness doesn't get in the way.
>
> In similar vein, I've used tuples internally in my Python code as
> cache keys for function memoizing. They are WORM structures - write
> once, read many - built to represent the cache value, but never
> updated.
> With this idea of tuples as a data structure, I could reasonably
> interpret this:
>
> (1,"abc",3) + [1]
>
> to result in (1,"abc",3,[1]) just as well as (1,"abc",3,1). But
> instead of just picking one, Python complains about this, and so
> forces me to explicitly use
>
> (1,"abc",3) + tuple([1])
>
> or
>
> (1,"abc",3) + ([1],)
>
> I don't think tuples are just an academic curiosity, as your post
> seems to suggest.
>
> -- Paul
Were lists implemented as efficiently as tuples in terms of memory and
speed, there would be no difference except the academic one--which is
perhaps important.
Actually, I must admit that I use them and their implicit meanings all
the time in my code (as proof, notice in the "any way to create a
table-like object?" thread how I specify column headers--its for a
reason I use tuple in the example because the resulting data structures
will be ordered according to the tuple passed--of course, I refuse to
enforce this via "ugly and boring" type checking). For the most part, I
ignore the implementation differences and draw the distinction at their
semantics.
Also, risking getting a reputation for being overly contentious, I think
your argument falls apart in the latter half of your post because "+",
as far as I can tell with python sequences, is used to mean catenate
exclusively and not append, which is what you suggest as a possibility.
Anyway, D'Arpano made an excellent point with a good mind-reading
example and so I understand the design decision regarding catenation of
tuples and lists more clearly now. I'm still not sure I like it, but
there is no accounting for taste.
James
More information about the Python-list
mailing list