Tuples -- who needs 'em
Gordon McMillan
gmcm at hypernet.com
Sat Apr 8 20:24:14 EDT 2000
Martijn Faassen wrote:
[discussing the need for tuples with Bob Alexander]
> Okay, but it's generally practiced in idiom.
[snip]
> ... I think it's more
> than just the dictionary key argument.
Very true. There are 2 technical arguments for tuples
(dictionary keys and efficiency); both are weak. The main
difference is idiomatic.
[Bob]
> > There have been cases where I used tuples, to find out
> > later that I wanted mutability, so went back and changed.
>
> Hm, I rarely if ever seem to run into this. If I need mutability of
> a set of heterogenous objects I tend to use a class. There may be a
> switch from tuples to classes, but not often from tuples to lists, I
> think.
That's because you understand the idiom, Martijn. Lists are
collections. The competing choice to a list is a dictionary.
Tuples are toy structs; the competing choice is a class.
Experience tells me that trying to use a list where a tuple is
needed will yield either unsafe or safe but bloated code. No
technical reason; it's just that lists (as collections) will
eventually be mutated in a way that's natural for collections,
but disastrous where the purpose is struct-like.
> > This sort of thing
> > happens in exploratory programming, so when in that mode one might want to
> > use lists unless one is really sure in advance that tuples are the right
> > thing.
By all means, use lists whenever you're unsure. You'll soon
come to recognize when a tuple is more appropriate. It won't
take long (it couldn't, Martijn's too young to have taken long to
learn anything <wink>).
- Gordon
More information about the Python-list
mailing list