What is the intended lifetime of a Python decorator?

两盒软妹~` 提交于 2019-12-04 14:52:50
Alexey Guseynov

Decorators' main purpose is not for temporary modification of function behaviour. The word "temporarily" is odd here. It is better to think about them as decomposition of responsibilities and code reuse. They are mostly used to add some behaviour which is not uniquely bound to decorating function and can be used for other functions too. One good example is the @memoized decorator, which remembers the value computed by the decorated function (which may take a lot of time to compute) and next time returns the value immediately. Such a decorator can be reused many times and, besides that, it makes core much more readable: instead of bloating your function with memoization code you add a single line which clearly states what actually happens.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!