[Tutor] adding methods to native types

Sean 'Shaleh' Perry shalehperry@attbi.com
Thu Feb 6 00:21:01 2003


On Wednesday 05 February 2003 21:16, Erik Price wrote:
> On Wednesday, February 5, 2003, at 11:43  PM, Sean 'Shaleh' Perry wrote=
:
> > The nice thing about functions is you can pass any object to them.
> > len() works
> > on any object that supports __len__().
>
> Strangely, it is more reassuring knowing that len() is just calling
> __len__().  I suppose because at least then the element-counting
> functionality *is* encapsulated within the class, which is where [I
> think] it should be.
>

true enough.

>
> Guess so!  But if the len() function simply calls the __len__() method,
> why make that method "magic" at all?  Why not just offer it as a
> regular method?  [To me] there's nothing magic about using a method to
> get some data about an object.
>

easy answer: it has been that way since Python began.

much of python lets you interact with objects without actually using meth=
ods. =20
I think len() is one of those vestiges.

I wonder if it had something to do with trying to ease people into OO (Py=
thon=20
was meant to help those learning programming).