Decorator help

Joseph L. Casale jcasale at
Sun Mar 31 01:06:01 CET 2013

> When you say "class vars", do you mean variables which hold classes?

You guessed correctly, and thanks for pointing out the ambiguity in my references.

> The one doesn't follow from the other. Writing decorators as classes is 

> fairly unusual. Normally, they will be regular functions.

I see, this I didn't know. I'll stick to this guideline now.

> A more complicated case is where you need to do some pre-processing, and 
> you *don't* want that calculation repeated every time the method is
> called. Decorators are fantastic for that case too, but here you cannot
> access instance attributes, since the instance doesn't exist yet. But you
> can access *class attributes*, as more-or-less ordinary local variables
> *inside* the class definition. Here's a working sketch of the sort of
> thing you can do. Copy and paste the following into a Python interactive
> session, and then see if you can follow what is being done when.

> Is your mind boggled yet? :-)

That was some of the coolest stuff I have seen a while. I had to wait until I had
enough time to actually run this through and utilize it my own work. I haven't
enjoyed Python this much since I first started using it.

Can't thank you enough for the time and thorough example, that imparted loads
of insight.


More information about the Python-list mailing list