Range Operation pre-PEP
nessus at mit.edu
Thu May 10 23:21:58 EDT 2001
"Tim Peters" <tim.one at home.com> writes:
> As explained in the docs, all sequence types are supposed to support those
> specific operations (along w/ a few others).
Well, then, if tuples are sequences, then one shouldn't say that it is
inappropriate to treat them as sequences. If it is inappropriate to
perform sequences operations on them, then they shouldn't be
sequences. If a tuple isa sequence, then all sequence operations
should be considered appropriate.
Personally, I consider a tuple to be a full-fledged sequence. It's an
immutable sequence, while a list is a mutable sequence. Mutability is
the important distinction between a list and a tuple.
> > In light of this, it seems that the fact that they are missing
> > count() and index() should only been seen as an unfortunate
> > oversight.
> But those aren't part of the sequence protocol (or "interface", if
> you like) defined by the docs. Sequence types may or may not choose
> to implement them. Tuples choose not to, and Guido has firmly
> rejected at least one working patch that sought to add those methods
> to tuples. Ben is channeling Guido's intent accurately here!
And what's that intent? Ben claimed that tuples should only be used
when you know how many elements there will be, and that lists should
only be used for homogeneous data of unknown length. The more
straight forward conclusion would be that tuples should be used when
you want an immutable sequence and that lists should be used when you
want a mutable sequence.
> > Or actually, tuples shouldn't be sequences at all, since you should
> > never treat a tuple as a sequence, rather than just as a record.
> As above, the Language Reference manual's idea of what "a sequence"
> is doesn't match yours; so while you're entitled to say tuples
> shouldn't be considered to be Douglas-sequences, claiming they're
> Python-sequences isn't really open to debate.
You misunderstand. I think that that a tuple *should* be considered
sequence. A full-fledged one. Not one restricted to data of length
known in advance. Not restricted to heterogeneous data. A tuple
should be considered a full-fledged immutable sequence of arbitrary
Python objects. No more, no less.
More information about the Python-list