带参数的装饰器

时间:2020-02-08 22:52:43

标签: python

def Decorator(*pos, **kwargs):
    def innerFunc(arg, num):
        arg()
        print('Inner function')
        print(kwargs['name'])
    return innerFunc

@Decorator(name = 'Michael')
def inputFunc():
    print('Input Function')

为什么将Decorator应用于inputFunc会自动调用它? 我想向装饰函数传递一些参数,但不能,因为装饰器已经调用了它

1 个答案:

答案 0 :(得分:-3)

在您的示例中,删除arg()调用:

def Decorator(*pos, **kwargs):
    def innerFunc(arg, num):
        # arg()  <-- this is calling the function
        print('Inner function')
        print(kwargs['name'])
    return innerFunc

@Decorator(name = 'Michael')
def inputFunc():
    print('Input Function')