热线电话:13121318867

登录
2018-12-13 阅读量: 806
怎么理解python的装饰器?

以下是有关Python中用于理解装饰器函数的函数的描述。

  1. 在Python中,我们可以在另一个函数中定义一个函数。
  2. 在Python中,函数可以作为参数传递给另一个函数(函数也可以返回另一个函数)。
  3. # A Python program to demonstrate that a function
  4. # can be defined inside another function and a
  5. # function can be passed as parameter.
  6. # Adds a welcome message to the string
  7. def messageWithWelcome(str):
  8. # Nested function
  9. def addWelcome():
  10. return "Welcome to "
  11. # Return concatenation of addWelcome()
  12. # and str.
  13. return addWelcome() + str
  14. # To get site name to which welcome is added
  15. def site(site_name):
  16. return site_name
  17. print messageWithWelcome(site("GeeksforGeeks"))

Python装饰器是一种删除冗余的强大工具。

0.0000
7
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子