Do deep inheritance trees degrade efficiency?

Christian Heimes lists at cheimes.de
Thu Mar 19 10:54:32 CET 2009


Chris Rebert wrote:
> There's no effect on attribute read-writes as they all take place
> within the single __dict__ of the instance. As for method lookup, it
> doesn't add an indirection per se, but rather the list of classes to
> look thru to find a method gets longer, making base-class method
> lookups slower. IIRC, a typical method lookup does something like the
> following pseudocode:
> 
> for klass in the_object.__mro__:
>     if method_name in klass.__dict__:
>         return klass.__dict__[method_name]
> raise AttributeError # no such attribute

Your assumption is no longer true. Starting with Python 2.6 and 3.0 the
lookup of attributes is cached. You can find detailed information by
searching for VERSION_TAG in the source code.

Christian




More information about the Python-list mailing list