decorators only when __debug__ == True

Steven D'Aprano steve at
Thu Apr 1 00:53:51 CEST 2010

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.

In this specific case, where the OP wants a "do nothing pass-decorator", 
he should do this:

def decorator(func):
    if __debug__:
        return func

rather than this:

def decorator(func):
    if __debug__:
        def inner(*args):
            return func(*args)
        return inner


More information about the Python-list mailing list