why no 'length' method in sequences ?

Paul Boddie paul at boddie.net
Fri Apr 19 10:08:35 EDT 2002


Just van Rossum <just at xs4all.nl> wrote in message news:<just-2DF829.09350419042002 at news1.xs4all.nl>...
> 
>     len(o)

[vs]

>     o.keys()
> 
> This seems quite arbitrary, even to me, after maybe 6 years of working 
> with Python. If o.__len__() were spelled o.len(), we could easily do 
> without the len() builtin. (Note that I don't argue we could do without 
> functions: I wouldn't want to miss them for the world...)

I suppose that len is considered near-universal, although len(123)
reveals a notable exception, whereas very few objects bear the notion
of having "keys". Therefore, near-universal "protocols" (as I believe
someone once called them) are supported through built-in functions,
and other "protocols" are supported more or less exclusively through
methods.

Of course, providing len as a method on all appropriate objects would
be more consistent, but as far as Python is concerned right now, it
seems redundant and potentially confusing: "But I thought len was a
function!" exclaimed the beginner.

Paul



More information about the Python-list mailing list