len が py3k でも 関数のままである理由

結局は なんだけど、ソースを提示しておく。

まず、Python FAQにはこんな風に書いてある。

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.

なんだけど、Python-3000 の仕様を決めるときに len() をメソッドにしようという話題が出て、Guido van Rossumが反対した。

(a) For some operations, prefix notation just reads better than
postfix -- prefix (and infix!) operations have a long tradition in
mathematics which likes notations where the visuals help the
mathematician thinking about a problem.
(b) When I read code that says len(x) I *know* that it is asking for
the length of something. This tells me two things: the result is an
integer, and the argument is some kind of container. To the contrary,
when I read x.len(), I have to already know that x is some kind of
container implementing an interface or inheriting from a class that
has a standard len().
Saying the same thing in another way, I see 'len' as a built-in
operation. I'd hate to lose that.
I didn't want these special operations to use ordinary
method names, because then pre-existing classes, or classes written by
users without an encyclopedic memory for all the special methods,
would be liable to accidentally define operations they didn't mean to
implement, with possibly disastrous consequences.


1. len()の様にいろいろな型が使う一般的な概念の操作は、オブジェクト指向的なメソッドよりも数学的な演算子・関数の方が判りやすい。

2. いろいろな型が使う一般的な操作が、通常の名前を使ってしまうと、その名前を知らないユーザーがその名前を使ってしまう。