python gripes survey

Robert Kern kern at taliesen.caltech.edu
Sun Aug 24 22:52:54 EDT 2003


In article <d8778a53.0308241450.1a0d6645 at posting.google.com>,
	pruebauno at latinmail.com (nnes) writes:

[snip]

> I have never understood why python has [].count() or "".count() but
> len([]) and len("").
> 
> Why not make len, sum, str, repr, etc just methods of the base object
> class with specific implementations for different types. Instead of
> doing len([]) we could do [].len().

Like John said, "mostly history."

This is in the General FAQ, question 4.6, in fact.

http://tinyurl.com/l2gw

"""4.6   Why does Python use methods for some functionality (e.g.
list.index()) but functions for other (e.g. len(list))?

The major reason is history. Functions were used for those operations
that were generic for a group of types and which were intended to work
even for objects that didn't have methods at all (e.g. tuples). It is
also convenient to have a function that can readily be applied to an
amorphous collection of objects when you use the functional features of
Python (map(), apply() et al).

In fact, implementing len(), max(), min() as a built-in function is
actually less code than implementing them as methods for each type. One
can quibble about individual cases but it's a part of Python, and it's
too late to make such fundamental changes now. The functions have to
remain to avoid massive code breakage.

Note that for string operations Python has moved from external functions
(the string module) to methods. However, len() is still a function.
"""

> This is actually possible already in part. I think you can do
> [].__len__(). But I never understood the reasoning behind making these
> special __x__() methods instead of doing a plain x() and using it like
> obj.x().

I think one of the benefits is that it promotes generic programming. If
I'm designing a class which acts like a sequence or collection, the
canonical way for me to expose the length or size of the object is to
define a __len__ method. If I'm using someone else's class which acts
like a collection, I know that I can almost certainly call len(obj) and
get the length. I don't have to remember if they called it .len(),
.length(), .getLength(), .size(), .cardinality(), .length (as an
attribute or property), .numKeys(), etc. And each one of those would be
used somewhere (exclusively) even if all of the builtins used only
.len().

> The only case I can think of, where it would look kind of odd would be
> floating points like:
> 
> niceoutput=2.3.str()

Actually it works fine for floats. Try it in the interpreter (using
__str__ or one of the other magic method, though).

Plain ints, on the other hand, are problematic.

> Anyway since Pythonistas are smart, it can not be a wart of the
> language and must be some lack in my understanding :)
> 
> awaiting-to-be-enlightened-yours

In fine Zen koan  tradition, consider yourself whacked on the head with
a large stick. :-)

> Nestor

-- 
Robert Kern
kern at caltech.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list