why no 'length' method in sequences ?

John Roth johnroth at ameritech.net
Fri Apr 19 17:52:38 EDT 2002


"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





More information about the Python-list mailing list