Range Operation pre-PEP
tim.one at home.com
Fri May 11 03:23:47 CEST 2001
>> Lists also have count() and index() methods, which tuples do not.
>> Doesn't this suggest a difference in intended purpose to you?
> I also see that tuples support "in" and "+" and "*" and slicing and
> len() and min() and max().
As explained in the docs, all sequence types are supposed to support those
specific operations (along w/ a few others).
> 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!
>>> filter() on a tuple returns a tuple. The length of the tuple cannot
>>> be known in advance.
>> Presumably filter() only requires its argument to be a sequence.
> Yes, if its sequence argument is a tuple, then it returns a tuple.
Unfortunate but true. Strings are special-cased by filter() too. Nothing
else is -- and a maze of inconsistent special cases is un-Pythonic on the
face of it.
> If you were right, you should never want to run filter on a tuple,
Indeed, I never have <0.1 wink>.
> and if you were so foolish to use filter() on a tuple, it should
> return a list to show you the errors of your ways.
That would be better. The filter() implementation was contributed code, and
snuck in along with map(), reduce() and lambda. If Guido had it to do over
again, I doubt he'd accept that patch; they're his answer to the question
"what do you like least about Python?".
> 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.
hmm-now-i'm-wondering-what-this-msg-was-about<wink>-ly y'rs - tim
More information about the Python-list