why no 'length' method in sequences ?

John Roth johnroth at ameritech.net
Sat Apr 20 07:03:50 EDT 2002


"Richard Gruet" <rjgruet at yahoo.com> wrote in message
news:a9q8f4$svn$1 at aphrodite.grec.isp.9tel.net...
>
> "John Roth" <johnroth at ameritech.net> wrote in message
> news:uc14cjalpru3b5 at news.supernews.com...
> >
> > "Richard Gruet" <rjgruet at yahoo.com> wrote in message
> > news:a9nbuq$916$1 at aphrodite.grec.isp.9tel.net...
> > > Alex
> > >
> > > And BTW, people would not to have implement any __len__ method if
all
> > the
> > > sequence types would derive from a common ancestor (sequence), in
> > which the
> > > (in)famous method would be implemented. Would it be completely
> > nonsensical ?
> > > I have been told that starting from 2.2, new style classes
(including
> > most
> > > built-in types) all derive from a common base class 'object'.
> >
> > As far as I know, that's correct. Inserting a .len() method in the
> > object class would automatically make it available to all new style
> > classes.
> >
> > As far as I can see, there is one major issue with this: if the
> > class in question didn't have a __len__() method, it would
> > still have the (inherited) len() method. This could be rigged to
> > raise an exception, but it would still be a bit of a trap for the
> > unwary.
> >
> > There's also the question of the inheritance hierarchy, which I
don't
> pretend to understand.
> > It seems to be a case of simple implementation, small benefit, small
> downside, no discernable side effects. Write a PEP.
> >
> > John Roth
>
> Actually, as Alex pointed out, a single len() method in a hypothetical
> 'sequence' base class would be mythical, simply because there is no
common
> implementation of len() for all kinds of collection (I just forgot
this!).

It doesn't matter. As I clearly implied, the default .len() method
would call the class's .__len__() method. Therefore, there is no
need for a common implementation.

> However we could still define a sort of abstract len() method  in a
> 'sequence' (or 'collection') base class, that has to be implemented in
> derived classes (or will raise an exception otherwise).

Why bother?

AFIC, it's still a yawner. Small benefit, small cost.

John Roth





More information about the Python-list mailing list