Why Do We Use This?

Alex cut_me_out at hotmail.com
Tue Jun 6 17:53:54 EDT 2000


> Okay, what is the difference between these?
> 
> If these are same, whey do we bother using the "long" version such as
> __len__(a)??

As far as I know, there is no reason to use __len__ (a), except to
access an overriden method of a subclass.  If a class A defines a __len__
method, that is what ends up being called when the function len is
called with an instance of A.

Sorry for the clumsy sentence.  Perhaps this example will help:

>>> class eg:
...  def __len__ (self):
...   print 'In the __len__ method, now'
...   return 1729
... 
>>> t = eg ()
>>> print len (t)
In the __len__ method, now
1729
>>> 

Alex.



More information about the Python-list mailing list