performance of dictionary lookup vs. object attributes

Fredrik Lundh fredrik at pythonware.com
Fri Aug 25 08:52:18 EDT 2006


Andre Meyer wrote:

> Is the test meaningful and are you surprised by the results?

surprised by the amount of code you needed to test this, at least.  and you
might wish to use the proper spelling for

                v = self.obj.__getattribute__(a)

which is

                v = getattr(obj, a)

and is quite a bit faster, at least in CPython.

(you should also use setattr() instead of __setattr__; in general, if you find your-
self *calling* a method named __method__, there's most likely a better way to
do it).

> 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().

</F> 






More information about the Python-list mailing list