why len(list) instead of list.len() ?

John Roth johnroth at ameritech.net
Tue Jun 18 07:41:36 EDT 2002


"Markus Jais" <mjais at web.de> wrote in message
news:EhBP8.1$Qn1.1124 at news.ecrc.de...
> hello
>
> I way wondering why one has to write
>
> list = [2, 3, 5]
> print len(list)
>
> instead of list.len()

This has come up before, many, many times. There
are two answers. One is that there is, in fact, a
list method to return a length. It is list.__len__().
The len() global function calls this method.
All objects that implement the len() function
do it by implementing this 'magic method.'

The other is that what feels intuitive to one
person doesn't necessarily feel intuitive to
someone else with a different background, etc.

John Roth






More information about the Python-list mailing list