What should a decorator do if an attribute already exists?

MRAB python at mrabarnett.plus.com
Tue May 10 13:42:45 EDT 2016


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?
>
> This. Your decorator should, IMHO, treat the attribute as private data,
> and if something else is using the same thing, something has clearly
> gone wrong and raising the error early and clearly is right.
>
If it's not clear what you should do, you could look at the Zen. It 
says: "In the face of ambiguity, refuse the temptation to guess." To me, 
that suggests raising an exception.




More information about the Python-list mailing list