summaryrefslogtreecommitdiff
path: root/Doc
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-07-30 22:58:12 -0700
committerGitHub <noreply@github.com>2018-07-30 22:58:12 -0700
commitdc9039da239ee572eaaf56e4a026be1fc4d74e24 (patch)
treebcde9aca9c7caee0dcf556a6f1b861d0f4c44277 /Doc
parent5e980f09b624b98bc1202cdabaaa0809c8b6437e (diff)
downloadcpython-git-dc9039da239ee572eaaf56e4a026be1fc4d74e24.tar.gz
bpo-27671: Update FAQ about why len is function (GH-8432)
(cherry picked from commit c48e26dcadbff8620bb5881d3bd148fc8894d0ef) Co-authored-by: INADA Naoki <methane@users.noreply.github.com>
Diffstat (limited to 'Doc')
-rw-r--r--Doc/faq/design.rst37
1 files changed, 19 insertions, 18 deletions
diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst
index 285c7f12b9..8588daaa2f 100644
--- a/Doc/faq/design.rst
+++ b/Doc/faq/design.rst
@@ -226,24 +226,25 @@ Python file objects support the iterator protocol, so you can now write simply::
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()``, ``zip()`` 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.
-
-.. XXX talk about protocols?
-
-.. note::
-
- For string operations, Python has moved from external functions (the
- ``string`` module) to methods. However, ``len()`` is still a function.
+As Guido said:
+
+ (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. Compare the easy with which we
+ rewrite a formula like x*(a+b) into x*a + x*b to the clumsiness of
+ doing the same thing using a raw OO notation.
+
+ (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(). Witness the confusion we occasionally have when
+ a class that is not implementing a mapping has a get() or keys()
+ method, or something that isn't a file has a write() method.
+
+ -- https://mail.python.org/pipermail/python-3000/2006-November/004643.html
Why is join() a string method instead of a list or tuple method?