在python中进行装饰时返回函数的原因是什么?

在python中进行装饰时返回函数的原因是什么?,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我想用python装饰一个函数,但我不知道为什么要在代码末尾返回外部函数 def outer_function(): word = "hi" def inner_function(): print(word) return inner_function outer_function() 装饰者背后的想法是,装饰一个功能。这意味着,您有一个函数,并且希望在不更改函数本身的情况下扩展或稍微修改它的行为。由于SO不是一般介绍Python和具体介绍decorators的

我想用python装饰一个函数,但我不知道为什么要在代码末尾返回外部函数

def outer_function():
   word = "hi"
   def inner_function():
       print(word)
   return inner_function
outer_function()

装饰者背后的想法是,装饰一个功能。这意味着,您有一个函数,并且希望在不更改函数本身的情况下扩展或稍微修改它的行为。由于SO不是一般介绍Python和具体介绍decorators的地方,因此这里有一个简单的decorator示例和一篇非常好的RealPython文章的链接,可以更深入地解释它们

def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

@my_decorator
def say_whee():
    print("Whee!")
链接:


A希望这对你有帮助。祝你今天愉快

那你为什么还要装饰一个功能呢?您希望完成什么?我已经将您的代码转换为代码格式,请修复缩进。目前您只返回内部函数,代码中没有装饰符。并且存在多个语法错误。您编写的代码不是函数装饰程序。因此,它并不是Python语言的入门。