【问题标题】:python decorator if sentencepython装饰器if语句
【发布时间】:2019-07-04 12:03:52
【问题描述】:

python 装饰器 if 语句

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)

구분어떻게해? 뭐야?`在此处输入代码 구분어떻게해?

【问题讨论】:

  • StackOverflow 是一个英文网站。可以用英文提问吗?

标签: python decorator


【解决方案1】:
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)

구분어떻게해? 나는 바보다 구분어떻게해?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-18
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    相关资源
    最近更新 更多