Re: What should a decorator do if an attribute already exists?

2016-05-16 Thread Chris Angelico
On Tue, May 17, 2016 at 3:05 PM, Steven D'Aprano wrote: > Although the instrumentation is used inside the decorator, it is actually part > of the public API for the function. So the user can do this: > > > @decorate > def myfunction(): > ... > > > # later

Re: What should a decorator do if an attribute already exists?

2016-05-16 Thread Steven D'Aprano
On Monday 16 May 2016 23:06, Kevin Conway wrote: >> I have a decorator that adds an attribute to the decorated function > > I might try to argue that this is not actually a decorator or, at least, > this is not a great decorator pattern for Python. Adding the attribute to > the function object

Re: What should a decorator do if an attribute already exists?

2016-05-16 Thread Steven D'Aprano
On Monday 16 May 2016 22:20, jmp wrote: > On 05/10/2016 05:45 PM, Steven D'Aprano wrote: >> I have a decorator that adds an attribute to the decorated function: > [snip] >> I think 5 is clearly wrong, 4 is too difficult, and 3 seems pointless. So I >> think either 1 or 2 is the right thing to do.

Re: What should a decorator do if an attribute already exists?

2016-05-16 Thread Steven D'Aprano
On Monday 16 May 2016 18:14, Francesco Loffredo wrote: > On 10/05/2016 17:45, Steven D'Aprano wrote: >> I have a decorator that adds an attribute to the decorated function: [...] >> My question is, what should I do if the decorated function already has an >> instrument attribute? > From your

Re: What should a decorator do if an attribute already exists?

2016-05-16 Thread Kevin Conway
> I have a decorator that adds an attribute to the decorated function I might try to argue that this is not actually a decorator or, at least, this is not a great decorator pattern for Python. Adding the attribute to the function object implies you need to access it at some later point. If so

Re: What should a decorator do if an attribute already exists?

2016-05-16 Thread jmp
On 05/10/2016 05:45 PM, Steven D'Aprano wrote: I have a decorator that adds an attribute to the decorated function: [snip] I think 5 is clearly wrong, 4 is too difficult, and 3 seems pointless. So I think either 1 or 2 is the right thing to do. Thoughts? It depends if the attribute

Re: What should a decorator do if an attribute already exists?

2016-05-16 Thread Francesco Loffredo
On 10/05/2016 17:45, Steven D'Aprano wrote: I have a decorator that adds an attribute to the decorated function: def decorate(func): instrument = make_instrument() @functools.wraps(func) def inner(*args): instrument.start() result = func(*args)

Re: What should a decorator do if an attribute already exists?

2016-05-10 Thread Michael Selik
On Tue, May 10, 2016 at 11:48 AM Peter Otten <__pete...@web.de> wrote: > Steven D'Aprano wrote: > > I have a decorator that adds an attribute to the decorated function: > >inner.instrument = instrument > >return inner > > the original instrument is still accessible as

Re: What should a decorator do if an attribute already exists?

2016-05-10 Thread Ethan Furman
On 05/10/2016 08:45 AM, Steven D'Aprano wrote: I have a decorator that adds an attribute to the decorated function: My question is, what should I do if the decorated function already has an instrument attribute? If the decorator is adding an attribute for the decorated thing to use, and

Re: What should a decorator do if an attribute already exists?

2016-05-10 Thread MRAB
On 2016-05-10 17:06, Stephen Hansen wrote: On Tue, May 10, 2016, at 08:45 AM, Steven D'Aprano wrote: I have a decorator that adds an attribute to the decorated function: [...] My question is, what should I do if the decorated function already has an instrument attribute? 1. raise an exception?

Re: What should a decorator do if an attribute already exists?

2016-05-10 Thread Peter Otten
Steven D'Aprano wrote: > I have a decorator that adds an attribute to the decorated function: > > > def decorate(func): > instrument = make_instrument() > > @functools.wraps(func) > def inner(*args): > instrument.start() > result = func(*args) >

Re: What should a decorator do if an attribute already exists?

2016-05-10 Thread Stephen Hansen
On Tue, May 10, 2016, at 08:45 AM, Steven D'Aprano wrote: > I have a decorator that adds an attribute to the decorated function: > [...] > My question is, what should I do if the decorated function already has an > instrument attribute? > > 1. raise an exception? This. Your decorator should,