python装饰器如果句子

时间:2019-07-04 12:03:52

标签: python decorator

Python装饰器(如果有句子)

def extrafun(hi):
    if hi>10:
        def decorator(func):
            def wrapper(x):
                print('before decora`enter code here`oting')
                func(x)`enter code here`
                print('After decoraoting')
            return wrapper
        return decorator

@extrafun(12)
def newfunc(c):
    print("this is new function")

# @extrafun(9)
# def newfunc(c):
#     print("뭐야?")
# error
def extrafun(hi):
    if hi>10:
        def decorator(func):
            def wrapper(x):
                print('before decoraoting')
                func(x)
                print('After decoraoting')
            return wrapper
        return decorator
    else:`enter code here`
        def decorator(func):
            def wrapper(x):
                print('구분어떻게해?')
                func(x)
                print('구분어떻게해?')
            return wrapper
        return decorator
@extrafun(12)
def newfunc(c):
    print("this is new function")

@extrafun(9)
def newfunc(c):
    print("뭐야?")

newfunc(12)

구분어떻게해? 뭐야? 구분어떻게해?

In [14]:

newfunc(9)

구분어떻게해? here在这里输入代码 구분어떻게해?

1 个答案:

答案 0 :(得分:0)

def extrafun(hi):
    if hi>10:
        def decorator(func):
            def wrapper(x):
                print('before decoraoting')
                func(x)
                print('After decoraoting')
            return wrapper
        return decorator
    else:
        def decorator(func):
            def wrapper(x):
                print('구분어떻게해?')
                func(x)
                print('구분어떻게해?')
            return wrapper
        return decorator
@extrafun(12)
def newfunc(c):
    print("this is new function")

@extrafun(9)
def newfunc2(c):
    print("나는 바보다")
newfunc(12)

装饰之前 这是新功能 装饰后

newfunc2(9)

구분어떻게해? 보다 구분어떻게해?