Discuss / Python / last train

last train

Topic source

叉烧叉烧

#1 Created at ... [Delete] [Delete and Lock User]

def log(*text):

    if str(type(text[0])) == "<class 'function'>":

        def wrapper(*arg):

            print("begin call")

            text[0](*arg)

            print("end call")

        return wrapper

    else:

        def decorator(fn):

            def wrapper(*arg):

                print("begin call")

                for t in text:

                    print(t)

                fn(*arg)

                print("end call")

            return wrapper

        return decorator

@log('有参数')

def f():

    pass

f()

begin call

有参数

end call

@log

def f():

    pass

f()

begin call

end call


  • 1

Reply