decorators only when __debug__ == True

MRAB python at mrabarnett.plus.com
Wed Mar 31 19:27:51 EDT 2010


Steven D'Aprano wrote:
> On Wed, 31 Mar 2010 22:27:05 +0100, MRAB wrote:
> 
>> LX wrote:
> [...]
>>> It looks to me the call stack still includes the additional level of
>>> the decorator... what am I missing? Thank you for your time.
>> Are you still defining your decorators in the same way as in your
>> original post?
>>
>> A decorator shouldn't call the function it's decorating.
> 
> *raises eyebrow*
> 
> Surely, in the general case, a decorator SHOULD call the function it is 
> decorating? I'm sure you know that, but your wording is funny and could 
> confuse the OP.
> 
What I mean is that the function that's doing the decorating shouldn't
call the function; it's the locally-defined wrapper function that calls
the decorated function.

For example, in my version of trace_decorator() it's show() that calls
the decorated function, not trace_decorator() itself.

Unless the word 'decorator' refers to the locally-defined function, in
which case, what do you call the function that does the wrapping, the
one whose name follows the '@'?

> In this specific case, where the OP wants a "do nothing pass-decorator", 
> he should do this:
> 
> def decorator(func):
>     if __debug__:
>         ...
>     else:
>         return func
> 
> rather than this:
> 
> def decorator(func):
>     if __debug__:
>         ...
>     else:
>         def inner(*args):
>             return func(*args)
>         return inner
> 
> 
> 




More information about the Python-list mailing list