What should a decorator do if an attribute already exists?

jmp jeanmichel at sequans.com
Mon May 16 08:20:35 EDT 2016


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 "instrument" is part of the public interface.

If not, just find a different name unlikely to clash with an existing 
one *and* raise an exception if it does ever happen anyway.

If the attribute is part of the public interface but you re using the 
code only internally then 1/ raise an exception, otherwise I don't know :o)

It seems to me that over-writing silently (or not) may lead to bugs 
difficult to spot.

jmp




More information about the Python-list mailing list