len() and PEP 3000

Kay Schluehr kay.schluehr at gmx.net
Wed Dec 6 09:44:01 EST 2006


Bjoern Schliessmann schrieb:

> Thomas Guettler wrote:
>
> > I suggest that at least lists, tupples, sets, dictionaries and
> > strings get a len() method.
>
> Why?

Pro: Because it makes the API more monotonous and more aligned with all
other OO languages that exist now and in future. It also helps any
written and unwritten IDE providing a method by means of
autocompletion. It ends endless debates with Java/C++/C# etc. and
newbie Python programmers about this minor issue.

Contra: Having both __len__ ( for providing a generic function by
duality ) and len in the API is unpleasant.

But maybe one can drop all __special__ methods and use a decorator to
express duality between methods and functions?

class M:
    @generic
    def foo(self):
         print "foo"

>>> foo(M())  #  equals M().foo()
"foo"

And since we are at it. Why not also dropping __add__, __radd__,
__plus__ etc. for:

class M:
    def binary+(self, other):   # replaces __add__
         ...

    def r_binary+(self, other): # replaces __radd__
         ...

    def unary+(self, other):    # replaces __plus__
         ...




More information about the Python-list mailing list