functions vs methods

MRAB python at mrabarnett.plus.com
Sun Jul 22 11:49:03 EDT 2018


On 2018-07-22 10:08, Ben Finney wrote:
> 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.
> 
Doesn't it have to be implemented on each type anyway?

The advantage is that it's enforces consistency.

In Java (or is it C#?) I seem to remember that some classes have a 
.Length attribute, some have a .Length() method, and some have a 
.Count() method.

In Python they're all len(...).



More information about the Python-list mailing list