【问题标题】:While working with Python Decorator i saw that function returns itself, what this logically implies? [duplicate]在使用 Python Decorator 时,我看到该函数返回自身,这在逻辑上意味着什么? [复制]
【发布时间】:2017-06-11 13:04:57
【问题描述】:

假设装饰器示例是

def decor(func):
    def wrap():
        print("=======")
        func()
        print("=======")
    return wrap

我知道一个函数可以返回任何变量输出比如 return a, return b+c ,它也可以返回另一个函数,但是函数返回自己,无法理解这个! 请尽可能简单地解释,因为我是自学者

【问题讨论】:

  • 该代码中没有返回自身的函数。
  • 在您的情况下,您误解了缩进在 Python 中的工作方式。 decor 是返回 wrap

标签: python functional-programming python-decorators


【解决方案1】:

如果你的意思是函数wrap本身返回wrap,那就不正确了。

查看缩进,decor 返回wrapwrap 返回None

【讨论】:

  • 我想我误解了这个缩进
猜你喜欢
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 2020-09-24
  • 2013-07-03
  • 2020-05-31
  • 1970-01-01
  • 2019-08-07
  • 2018-01-19
相关资源
最近更新 更多