The Python Oracle

Why does Python code use len() function instead of a length method?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle2

--

Chapters
00:00 Question
00:24 Accepted answer (Score 197)
01:04 Answer 2 (Score 110)
02:48 Answer 3 (Score 40)
04:59 Answer 4 (Score 36)
05:13 Thank you

--

Full question
https://stackoverflow.com/questions/2371...

Accepted answer links:
[len()]: http://www.python.org/doc/2.5.2/lib/buil...
[Emulating container types]: http://www.python.org/doc/2.5.2/ref/sequ...
[Python and the Principle of Least Astonishment]: http://lucumr.pocoo.org/2011/7/9/python-.../

Answer 2 links:
[this question]: https://stackoverflow.com/questions/8398...

Answer 3 links:
[PyVarObject]: http://hg.python.org/cpython/file/8c8315...
[Python Data Model]: https://docs.python.org/3.4/reference/da...
[this one]: http://effbot.org/pyfaq/why-does-python-...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 203


Strings do have a length method: __len__()

The protocol in Python is to implement this method on objects which have a length and use the built-in len() function, which calls it for you, similar to the way you would implement __iter__() and use the built-in iter() function (or have the method called behind the scenes for you) on objects which are iterable.

See Emulating container types for more information.

Here's a good read on the subject of protocols in Python: Python and the Principle of Least Astonishment




ANSWER 2

Score 117


Jim's answer to this question may help; I copy it here. Quoting Guido van Rossum:

First of all, I chose len(x) over x.len() for HCI reasons (def __len__() came much later). There are two intertwined reasons actually, both HCI:

(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.

Saying the same thing in another way, I see ‘len‘ as a built-in operation. I’d hate to lose that. /…/




ANSWER 3

Score 36


There is a len method:

>>> a = 'a string of some length'
>>> a.__len__()
23
>>> a.__len__
<method-wrapper '__len__' of str object at 0x02005650>



ANSWER 4

Score 11


met% python -c 'import this' | grep 'only one'
There should be one-- and preferably only one --obvious way to do it.