why no 'length' method in sequences ?

Alex Martelli aleax at aleax.it
Thu Apr 18 15:55:11 EDT 2002


Richard Gruet wrote:

> Hi all
> 
> I wonder why there is no 'length' (or 'len') method - or attribute - in
> the sequences types. Having to use the built-in function len() doesn't
> sound very Object-Oriented to me. I understand that the built-in function
> len() must be kept for compatibility reasons, but why not to add also the
> method len() to the sequence types ? Is there a reason ?

I would be a silly burden on people who want to implement a sequence
type, to make them define a method called len that is an exact synonym
for the existing special method __len__.  And it would be an intolerable
burden, conceptually and in terms of performance, if looking up an
attribute named 'len' performed strange black-magic contortions (even
worse if those contortions were specialized for only the cases in which
said attribute is meant to be called -- remember Python does not draw
deep distinctions between attributes meant to be called, and others).

It may not SOUND "very Object-Oriented", but having special methods
"clothed" by means of built-ins and operators is a clever and subtle
set-up, as I tried explaining in the other post to this thread.  And Python
has always preferred the steak to the sizzle, whenever choice was needed:
part of what makes it such a great language.


Alex




More information about the Python-list mailing list