解释Python装饰器是如何工作的

解释Python装饰器是如何工作的,python,decorator,Python,Decorator,这是python装饰器的一个示例。我无法理解它的工作方式。请给我解释一下给定示例的控制流。我将不胜感激 def helloSolarSystem(original_function): def new_function(*args, **kwargs): original_function(*args, **kwargs) print("Hello, solar system!") return new_function def helloGal

这是python装饰器的一个示例。我无法理解它的工作方式。请给我解释一下给定示例的控制流。我将不胜感激

def helloSolarSystem(original_function):
   def new_function(*args, **kwargs):
        original_function(*args, **kwargs)  
        print("Hello, solar system!")
   return new_function

def helloGalaxy(original_function):
    def new_function(*args, **kwargs):
        original_function(*args, **kwargs)  
        print("Hello, galaxy!")
    return new_function

@helloGalaxy
@helloSolarSystem
def hello(targetName=None):
     if targetName:
        print("Hello, " +  targetName +"!")
     else:
        print("Hello, world!")
hello("Earth")

修饰符是在Python中应用的语法糖。高阶函数是将一个或多个函数作为输入并返回函数的函数。i、 e

h(x) = f(g(x))
其中,
f()
是一个高阶函数,它接受一个单参数函数,
g(x)
,并返回一个单参数函数,
h(x)
。您可以将
f()
视为修改
g()
的行为

高阶函数是(根据定义),因此在您的特定示例中,decorator语法

@helloGalaxy
@helloSolarSystem
def hello(targetName=None):
    ...
相当于,

hello = helloGalaxy(helloSolarSystem(hello))
通过将
hello
替换为
helloLarSystem
,然后将其结果替换为
helloGalaxy
,我们得到等价的函数调用

def hello(targetName=None):
    if targetName:                            |        
        print("Hello, " + targetName + "!")   |  (1)  |
    else:                                     |       |  (2)   |
        print("Hello, world!")                |       |        |  (3)
    print("Hello, solar system!")                     |        |
    print("Hello, galaxy!")                                    |
其中(1)是原始
hello()
的应用,(2)是

def helloSolarSystem(original_function):
    def new_function(*args, **kwargs):
        original_function(*args, **kwargs)   <-- (1)
        print("Hello, solar system!")
    return new_function
def helloGalaxy(original_function):
    def new_function(*args, **kwargs):
        original_function(*args, **kwargs)   <-- (2)
        print("Hello, galaxy!")
    return new_function
def helloSolarSystem(原始功能):
def新功能(*args,**kwargs):

原始的_函数(*args,**kwargs)装饰符是在Python中应用的语法糖。高阶函数是将一个或多个函数作为输入并返回函数的函数。i、 e

h(x) = f(g(x))
其中,
f()
是一个高阶函数,它接受一个单参数函数,
g(x)
,并返回一个单参数函数,
h(x)
。您可以将
f()
视为修改
g()
的行为

高阶函数是(根据定义),因此在您的特定示例中,decorator语法

@helloGalaxy
@helloSolarSystem
def hello(targetName=None):
    ...
相当于,

hello = helloGalaxy(helloSolarSystem(hello))
通过将
hello
替换为
helloLarSystem
,然后将其结果替换为
helloGalaxy
,我们得到等价的函数调用

def hello(targetName=None):
    if targetName:                            |        
        print("Hello, " + targetName + "!")   |  (1)  |
    else:                                     |       |  (2)   |
        print("Hello, world!")                |       |        |  (3)
    print("Hello, solar system!")                     |        |
    print("Hello, galaxy!")                                    |
其中(1)是原始
hello()
的应用,(2)是

def helloSolarSystem(original_function):
    def new_function(*args, **kwargs):
        original_function(*args, **kwargs)   <-- (1)
        print("Hello, solar system!")
    return new_function
def helloGalaxy(original_function):
    def new_function(*args, **kwargs):
        original_function(*args, **kwargs)   <-- (2)
        print("Hello, galaxy!")
    return new_function
def helloSolarSystem(原始功能):
def新功能(*args,**kwargs):

原始函数(*args,**kwargs)这只是观察到在Python中,函数与其他任何东西一样都是对象。啊,包含变量的函数,你没那么特别

>>> issubclass(int, object) # all objects in Python inherit from a common baseclass
True
>>> def foo():
...     pass
>>> foo.__class__ # 1
<type 'function'>
>>> issubclass(foo.__class__, object)
True
>issubclass(int,object)#Python中的所有对象都从公共基类继承
真的
>>>def foo():
...     通过
>>>一级
>>>issubclass(对象类)
真的
以你为例,, @helloGalaxy表示hello=helloGalaxy(hello)

类似地,@helloSolarSystem意味着hello=helloSolarSystem(hello)

你打电话的时候

@极度松弛

@helloSolarSystem before hello()函数

hello将由helloGalaxy和HelloSalSystem装饰

因此,新函数()将被helloSolarSystem()覆盖


在调用hello(“Earth”)之前,hello函数由helloSolarSystem()的newFunction()和helloGalaxy()装饰器装饰。

这只是观察到的一点,即在Python中,函数与其他所有函数一样都是对象。啊,包含变量的函数,你没那么特别

>>> issubclass(int, object) # all objects in Python inherit from a common baseclass
True
>>> def foo():
...     pass
>>> foo.__class__ # 1
<type 'function'>
>>> issubclass(foo.__class__, object)
True
>issubclass(int,object)#Python中的所有对象都从公共基类继承
真的
>>>def foo():
...     通过
>>>一级
>>>issubclass(对象类)
真的
以你为例,, @helloGalaxy表示hello=helloGalaxy(hello)

类似地,@helloSolarSystem意味着hello=helloSolarSystem(hello)

你打电话的时候

@极度松弛

@helloSolarSystem before hello()函数

hello将由helloGalaxy和HelloSalSystem装饰

因此,新函数()将被helloSolarSystem()覆盖


在调用hello(“Earth”)之前,hello函数由helloSolarSystem()的newFunction()和helloGalaxy()装饰程序装饰。

这里有一个可能有用的小教程:这里有一个可能有用的小教程: