Discuss / Python / 记录一下

记录一下

Topic source

XHS_12302

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding: utf-8 -*-
import time, functools


# 使用自定义的改名装饰器

def my_wraps(origin):
    def decoretor(inner_wrapper):
        def out_wrapper(*args, **kw):
            return inner_wrapper(*args, **kw)
        out_wrapper.__name__ = origin.__name__
        return out_wrapper
    return decoretor


def metric(fn):
    # @functools.wraps(fn)
    @my_wraps(fn)
    def wrapper(*args, **kw):
        s = time.time()
        rst = fn(*args, **kw)
        e = time.time()
        print('%s executed in %s ms' % (fn.__name__, e-s))
        return rst
    # wrapper.__name__ = fn.__name__
    return wrapper  # wrapper <==> my_wrap(args)(wrapper) <==> out_wrapper


@metric 
def test_name():
    pass

f = test_name
print(f.__name__)



  • 1

Reply