Range Operation pre-PEP

Fredrik Lundh fredrik at pythonware.com
Fri May 11 09:28:09 CEST 2001


Douglas Alan wrote:
>
> I also see that tuples support "in" and "+" and "*" and slicing and
> len() and min() and max().  In light of this, it seems that the fact
> that they are missing count() and index() should only been seen as an
> unfortunate oversight.

more likely, it's because you don't know your python well enough.

the core sequence interface (PySequenceMethods) includes the
following methods:

    __len__
    __add__ (concat)
    __mul__ (repeat)
    __getitem__, __setitem__, __delitem__
    __getslice__, __setslice__, __delslice__

like most other operations that work on sequences, min() and max()
only require you to have working __len__ and __getitem__ methods.

none of the methods provided by list objects are part of the core
sequence protocol.

> Yes, if its sequence argument is a tuple, then it returns a tuple

which probably is an unfortunate oversight, since it returns lists
for all other sequences:

>>> from UserList import UserList
>>> L = UserList((1, 2, 3, 4))
>>> filter(None, L)
[1, 2, 3, 4]
>>> type(filter(None, L))
<type 'list'>

> If you were right, you should never want to run filter on a tuple, 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.  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.

since ben is right, I'm glad you're not in charge of python's design.

Cheers /F





More information about the Python-list mailing list