Discuss / Python / 可以带参,可以不带参

可以带参,可以不带参

Topic source

欧石楠

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

def log(text=""):

    if isinstance(text,str):

        def d(func):

            import functools

            @functools.wraps(func)

            def w(*args,**kw):

                print(f'text:{text},func.__name__:{func.__name__},begin call')

                result = func(*args,**kw)

                print(f'text:{text},func.__name__:{func.__name__},end call')

                return result

            return w

        return d

    import functools

    @functools.wraps(text)

    def w(*args,**kw):

        print(f'func.__name__:{text.__name__},begin call')

        result = text(*args,**kw)

        print(f'func.__name__:{text.__name__},end call')

        return result

    return w


  • 1

Reply