functions vs methods

Ben Finney ben+python at benfinney.id.au
Sun Jul 22 05:08:13 EDT 2018


INADA Naoki <songofacandy at gmail.com> writes:

> Please don't refer the FAQ entry.
> See this: https://bugs.python.org/issue27671

Interesting. Thanks for raising that bug report.

I offer my text as a starting point for a better explanation:

    Because ‘len’ works with *any* sequence, not only lists. To
    implement it as a method of each sequence type, it would have to be
    implemented on each type separately, which is a design that is
    needlessly more complex.

    This is common in Python: it uses so-called “duck typing”
    <URL:https://docs.python.org/3/glossary.html#term-duck-typing>,
    where the way an object behaves is more important than its type.
    Because “what is the length of this object” is a question valid for
    a broad variety of types, the design decision was made to allow it
    to accept any type for which that query makes sense.

Feel free to use that (or something derived from it) to improve the
documentation as you suggest.

-- 
 \       “Come on, if your religion is so vulnerable that a little bit |
  `\           of disrespect is going to bring it down, it's not worth |
_o__)               believing in, frankly.” —Terry Gilliam, 2005-01-18 |
Ben Finney



More information about the Python-list mailing list