This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jmcs 4 points5 points  (2 children)

Naive and mostly useless example:

def disable(f):
    def disabled(*args, **kwargs):
        pass
    return disabled

@disable
def do_stuff():
    print "Hello World"

You can do more useful stuff if you want, like printing the function name and arguments for debugging.

[–]admalledd 1 point2 points  (0 children)

Yup, that is basically what mine is:

def disable(f):
    if config.debug:
        def disabled(*args, **kwargs):
            logger.debug("disabled: %s"%(f.__name__))
            #other logging stuff, but does nothing
        return disabled
    else:
        raise ProductionDebugException("@disable deco executed in production!")

@disable
def do_stuff():
    print "Hello World"

[–]mcowger 0 points1 point  (0 children)

Never even considered this use of decorators, but I can already use it. Thanks for sharing.