What should a decorator do if an attribute already exists?

Chris Angelico rosuav at gmail.com
Tue May 17 01:19:38 EDT 2016


On Tue, May 17, 2016 at 3:05 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> 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
> myfunction.instrument.query()
>
> and see what data has been collected.
>
> So it is important that the name of the attribute be a public name.

Sounds to me like a collision is largely not your problem, so I'd go
with just tossing in a simple warning as a courtesy. There are plenty
of situations where that courtesy wouldn't be given, and the caller
would have to cope with the clash, so this would already be one up on
that. Not worth going to a heap of hassle to deal with it, as most
functions don't grow new attributes at a great rate. A quick check and
a warning, and that's plenty.

ChrisA



More information about the Python-list mailing list