A Python 3000 Question

Terry Reedy tjreedy at udel.edu
Mon Oct 29 17:49:59 EDT 2007


"brad" <byte8bits at gmail.com> wrote in message 
news:fg5fku$5c1$1 at solaris.cc.vt.edu...
| Will len(a_string) become a_string.len()?

No.

 I was just reading
| http://docs.python.org/dev/3.0/whatsnew/3.0.html

which says nothing about such a change, except for one in the opposite 
direction: o.next() changes to next(o) which in turn calls o.__next__(), 
just as len(o) calls o.__len__()

| One of the criticisms of Python compared to other OO languages is that
| it isn't OO enough or as OO as others or that it is inconsistent.

Python is object-based and has a nice user-defined type (class) system, but 
I do not believe Guido has ever called it object oriented.  So the 
comparision is besides the point even if true.

| Is there a reason that len cannot be a method?

It corresponds to and calls method .__len__ , when such exists.  Yes, 
Python could have been designed differently, with no builtin functions, but 
is was not.  Python is also a functional language with first-class generic 
functions.

| why not a_string.len()?

You are free to bypass builtins and call methods directly if you like:
a_string.__len__().

But consider rewriting the following:

def table(func, seq):
    return zip(seq, map(func,seq))

table(len, ('', (), []))

If you *really* want to be super-OO, like functionless OO languages, you 
can also call methods instead of using operator symbols, which in effect 
are names of builtin functions.

Instead of a+b, write a.__add__(b).  And so on.

Terry Jan Reedy






More information about the Python-list mailing list