What should a decorator do if an attribute already exists?

Stephen Hansen me+python at ixokai.io
Tue May 10 12:06:17 EDT 2016


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.

-- 
Stephen Hansen
  m e @ i x o k a i . i o



More information about the Python-list mailing list