python 修饰器

def wrapper1(function):
    print("In wrapper1 pre")
    @functools.wraps(function)
    def wrapper(*args, **kwargs):
        print("In wrapper1 wrapper")
        args = [float(arg) for arg in args]
        return float(function(*args, **kwargs))
    print("In wrapper1 post")
    return wrapper

def wrapper2(function):
    print("In wrapper2 pre")
    @functools.wraps(function)
    def wrapper(*args, **kwargs):
        print("In wrapper2 wrapper")
        args = [float(arg) for arg in args]
        return float(function(*args, **kwargs))
    print("In wrapper2 post")
    return wrapper

@wrapper1
@wrapper2                     
def mean(first, second, *rest):
    number = (first, second) + rest
    return sum(number) / len(number)
In wrapper2 pre
In wrapper2 post
In wrapper1 pre
In wrapper1 post
mean(1,'5')
In wrapper1 wrapper
In wrapper2 wrapper

相关推荐