Why can't Python decorators be chained across definitions?

前端 未结 3 1897
没有蜡笔的小新
没有蜡笔的小新 2021-02-09 01:43

Why arn\'t the following two scripts equivalent?

(Taken from another question: Understanding Python Decorators)

def makebold(fn):
    def wrapped():
             


        
3条回答
  •  没有蜡笔的小新
    2021-02-09 02:11

    The problem is replacing "makeitalic" (which takes one argument) with the "wrapped"-function in "makebold" which takes zero arguments.

    Use *args, **kwargs to pass on arguments further down the chain:

    def wrapped(*args, **kwargs):
        return "" + fn(*args, **kwargs) + ""
    

提交回复
热议问题