list.__len__() or len(list)

Ian Kelly ian.g.kelly at gmail.com
Tue May 13 20:18:01 EDT 2008


On Tue, May 13, 2008 at 5:57 PM, Nikhil <mnikhil at gmail.com> wrote:
>  __len__() is a built-in function of the list object and is updated along
> with the list object elements and will be useful incase the list is very
> huge.
>
>  len() is an external method again, which may require the processing cycles
> again.

The purpose of obj.__len__() is to implement len(obj), which simply
calls it.  So obj.__len__() may be faster, but only marginally.  The
reason to prefer len(obj) is that if you inadvertently pass an object
that does not implement __len__, you get the more appropriate
TypeError rather than an AttributeError.



More information about the Python-list mailing list