2019-07-01
阅读量:
537
python指定另一个函数的装饰器函数
装饰器是一个函数,它将函数作为唯一参数并返回一个函数。这有助于一遍又一遍地使用相同的代码“包装”功能。例如,上面的代码可以重写如下。
我们使用@func_name指定要应用于另一个函数的装饰器。
# Adds a welcome message to the string
# returned by fun(). Takes fun() as
# parameter and returns welcome().
def decorate_message(fun):
# Nested function
def addWelcome(site_name):
return "Welcome to " + fun(site_name)
# Decorator returns a function
return addWelcome
@decorate_message
def site(site_name):
return site_name;
# Driver code
# This call is equivalent to call to
# decorate_message() with function
# site("GeeksforGeeks") as parameter
print site("GeeksforGeeks")






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论