What should a decorator do if an attribute already exists?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue May 17 01:07:03 EDT 2016
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.
>>
>> Thoughts?
>
> It depends if the attribute "instrument" is part of the public interface.
Yes it is.
> 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.
At this point, I think I will raise a warning. The user can use the warnings
module to turn that warning into an error, if they want to, otherwise it's not
necessarily an error.
--
Steve
More information about the Python-list
mailing list