why no 'length' method in sequences ?

Just van Rossum just at xs4all.nl
Sat Apr 20 03:18:32 EDT 2002


In article <mailman.1019269858.27293.python-list at python.org>,
 com-nospam at ccraig.org (Christopher A. Craig) wrote:

> I have the utmost respect for Alex, so I don't understand why I can't
> figure out what he is saying, but why couldn't len(a) be equivalent to
> a.len()?

In a reply to my post, that either was private or never reached the 
group, he has one good argument *for* len as a function: you can't do 
the following easily without it:
   map(len, seq)
The equivalent with a method would be something like
   [i.len() for i in seq]
Indeed not as nice.

Still, I remain unconvinced also.

[ ... ]

> The main problem I see with this is that it wouldn't have worked
> before you got the ability to subtype in Python2.2, so historically it
> had to be a method before.  Plus adding it now means you either have a
              ^^^^^^
              "function", right?
> stub "len" method which may not work, or you have a major backward
> incompatibility.  

I don't think it's neccesarily the missing ability to subtype, but 
rather the huge difference between type and classes that simply would've 
made it much harder to give builtin types a [__]len[__] method 
accessible from Python. Not impossible, but harder. But I agree with you 
that len-as-a-function is probably a historic artefact.

> I personally disagree with adding it now, but not because it can't be
> done.

Well, it can't be done for b/w compatibility... But I know what you 
mean, and I agree on both counts.

Just



More information about the Python-list mailing list