简体   繁体   English

什么是python中的函数装饰器

[英]What is a function decorator in python

What is a function decorator? 什么是函数装饰器? Is it something we use to declare a function? 我们用来声明函数的东西吗? Or is it like a constructor. 还是像构造函数一样。

A function decorator is simply a function intended to take a function as an argument and return a new function to use in its place. 函数装饰器只是一个旨在将函数作为参数并返回一个新函数以代替其使用的函数。 Python provides decorator syntax to simply its use. Python提供了装饰器语法来简化其用法。 That is, 那是,

@foo
def bar():
    pass

is equivalent to 相当于

def bar():
    pass
bar = foo(bar)

The syntax takes care of applying the decorator to the original function and rebinding the result to the original name. 该语法负责将装饰器应用于原始函数,并将结果重新绑定到原始名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM