Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python decorator for multiple functions as arguments?

I get the basic principle of Python decorators and I like the syntax. However, they seem a little limited on what you can do.

Is there an elegant way to go about handling multiple functions as arguments like below?

def parent_fn(child_fn1, child_fn2):
    def wrapper():
        print('stuff is happening here')
        child_fn1()
        print('other stuff is happening here')
        child_fn2()
    return wrapper

@parent_fn
def decorated():
    print('child_fn1 stuff here')

decorated()

Where could I put the child_fn2 code? The few ideas I have tried appears to take away from the simplicity and elegance of the decorator.

like image 846
Michael Avatar asked Oct 25 '25 08:10

Michael


1 Answers

You could do that:

import functools

def sequence_with(f):
    def wrap(g):
        @functools.wraps(g)
        def sequenced_func():
            f()
            g()
        return sequenced_func
    return wrap

def func1():
    print('func1')

@sequence_with(func1)
def func2():
    print('func2')

but it's probably not a good conceptual fit for decorators. Conceptually, decorators are supposed to alter the functionality of a single function they decorate; a function that sequences two other functions on an equal footing may not make much sense as a decorator. For example, the new function created by the decorator is now under the name func2, replacing the original func2. That only makes sense for some specific use cases, whereas avoiding decorator syntax would give you more flexibility.

It makes more sense to not use decorator syntax:

def sequence(f, g):
   def sequenced_func():
       f()
       g()
   return sequenced_func

def func1():
    print('func1')

def func2():
    print('func2')

sequenced_func = sequence(func1, func2)
like image 150
user2357112 supports Monica Avatar answered Oct 26 '25 22:10

user2357112 supports Monica