這是一個python裝飾器的例子。我無法理解它的工作方式。請解釋給出的例子的控制流程。我會非常感激。解釋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")
下面是一個可能有所幫助的小教程:https://www.codementor.io/python/tutorial/introduction-to-decorators – Sheena