performance of dictionary lookup vs. object attributes

Duncan Booth duncan.booth at invalid.invalid
Fri Aug 25 11:02:05 EDT 2006


Fredrik Lundh wrote:

>> I am, actually, because I would have assumed that attribute access
>> with an object should be faster because lookup can be precompiled.
> 
> huh?  you're using a reflection API; there's no way the compiler can
> figure out in advance what you're going to pass to getattr().

That isn't necessarily entirely true. You could imagine a situation where 
at class definition time the interpreter makes a check for any method which 
accesses self.someattr and self is never reassigned in the method, and 
someattr is listed in __slots__. It could then create new code for those 
methods with (new) bytecodes LOAD_FAST_SLOT/STORE_FAST_SLOT which bypassed 
the normal lookup procedures.

Of course it would have to do some fancy footwork to get it right in all 
cases (and watch out for people trying to pull the function out of the 
class dict for reuse elsewhere), and any speedup might not be noticable, 
but it might be worth the experiment.




More information about the Python-list mailing list