On Wed, Mar 27, 2013 at 7:29 PM, Steven D'Aprano < steve+comp.lang.pyt...@pearwood.info> wrote:
> > The one doesn't follow from the other. Writing decorators as classes is > fairly unusual. Normally, they will be regular functions. If your > decorator needs to store so much state that it needs to be a class, > you're probably trying to do too much from a single decorator. > > There's more that you need to describe, such as what it is that the > decorator actually does, and whether it does it once, when the decorator > is called, or repeatedly, when the decorated method is called. > > The second case is the easiest. Suppose you have a class like this, with > many methods which have code in common. Here's a toy example: > > > def MyClass(object): > x = "class attribute" > > def __init__(self, y): > self.y = y > In the spirit of nit-picking, I'll point out that Steven meant to use the 'class' keyword instead of 'def' for MyClass. > def MyClass(object): > x = "class attribute" > > def __init__(self, y): > self.y = y > And here as well. It's potentially worth pointing out that this code will actually compile. It will even run, assuming you provide MyClass with a single argument. But it will always return None :). As per usual, the response was thorough and helpful -- I appreciate responses like these and how they've helped improve my command of Python. All the best, Jason
-- http://mail.python.org/mailman/listinfo/python-list