A question on python performance.
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Sep 28 04:04:04 EDT 2007
Joe Goldthwaite a écrit :
(snip)
> I guess I still think of decorators as the people
> who got the gym ready for the prom. I've tried getting up to speed on
> decorators but I haven't had much success.
The @decorator syntax is just syntactic sugar for a common use of higher
order functions. So to understand decorators, you first need understand
the concepts of higher order functions and closures. Both are documented
in many places on the web.
> Bruno Desthuilliers wrote:
>
>> IOW, direct access to obj.__class__.__dict__ bypasses both inheritence
>> and per-instance overriding.
>
> Thanks for your response also Bruno. I don't understand the code you
> posted well enough yet to even ask a useful question. There are a number
> of things I haven't seen before.
It's very 101 python code, apart from this line :
c2.dothis = dothat.__get__(c2, type(c2))
which is a way to dynamically attach a function as method to an
instance, directly invoking the descriptor protocol (it's documented on
python.org)
FWIW, it's most often done using the 'new' module, and possibly less
confusing:
import new
c2.dothis = new.instancemethod(dothat, c2, type(c2))
> Now I really feel like a newbie!
My fault - this example wasn't that necessary, the main point was wrt/
direct access to instance.__class__.__dict__ bypassing normal attribute
lookup rules so inherited attributes are not resolved...
> I'm working on it though so I may have some questions later.
You're welcome.
More information about the Python-list
mailing list