views:

75

answers:

3

Is it possible to decorate a function based on a condition?

a'la:

if she.weight() == duck.weight(): 
    @burn
def witch():
    pass

I'm just wondering if logic could be used (when witch is called?) to figure out whether or not to decorate witch with @burn?

If not, is it possible to create a condition within the decorator to the same effect? (witch being called undecorated.)

I appreciate any assistance in figuring this out.

A: 

It appears that the answer is yes.

http://wiki.python.org/moin/PythonDecoratorLibrary#Enable.2BAC8-DisableDecorators

MadSc13ntist
+1  A: 

You can create a 'conditionally' decorator:

>>> def conditionally(dec, cond):
    def resdec(f):
        if not cond:
            return f
        return dec(f)
    return resdec

Usage example follows:

>>> def burn(f):
    def blah(*args, **kwargs):
        print 'hah'
        return f(*args, **kwargs)
    return blah

>>> @conditionally(burn, True)
def witch(): pass
>>> witch()
hah

>>> @conditionally(burn, False)
def witch(): pass
>>> witch()
Claudiu
+1 I like this more than my solution, as it keeps with the decorator theme, and looks pretty clean, imo.
nilamo
+1  A: 

Decorators are just syntactical sugar for re-defining the function, ex:

def wrapper(f):
    def inner(f, *args):
        return f(*args)
    return lambda *args: inner(f, *args)

def foo():
    return 4
foo = wrapper(foo)

Which means that you could do it the old way, before the syntactical sugar existed:

def foo():
    return 4
if [some_condition]:
    foo = wrapper(foo)
nilamo