to pass self or not to pass self

Gregory Ewing greg.ewing at canterbury.ac.nz
Sat Mar 20 03:32:23 EDT 2010


Patrick Maupin wrote:

> Actually, I think I overstated my case -- there is some special logic
> for len and built-in objects, I think.

Yes, len() invokes the C-level sq_len slot of the type object,
which for built-in types points directly to the C function
implementing the len() operation for that type. So len() on
a string doesn't involve looking up a __len__ attribute or
creating a bound method at all.

The only reason a __len__ attribute exists at all for
built-in types is to give Python code the illusion that
C-coded and Python-coded classes work the same way. Most
of the time it's never used.

If you try the same test using a method that doesn't have
a corresponding type slot (basically anything without a
double_underscore name) you will probably see a small
improvement.

-- 
Greg



More information about the Python-list mailing list