is there any way to run a other function after a function run finish?

前端 未结 2 1901
慢半拍i
慢半拍i 2021-01-17 05:48
def foo():
    pass

def bar():
    print \'good bay\'

two function like blow and now i want to run bar function after foo run finish

is th

2条回答
  •  渐次进展
    2021-01-17 06:30

    Why not:

    foo()
    bar()
    

    ?

    In case you want to have some general way to do it, you can try:

    def run_after(f_after):
        def wrapper(f):
            def wrapped(*args, **kwargs):
                ret = f(*args, **kwargs)
                f_after()
                return ret
            return wrapped
        return wrapper
    
    @run_after(bar)
    def foo():
        ....
    

    This way bar is always run after foo is executed.

提交回复
热议问题