How can one attach a decorator to a function “after the fact” in python?

半城伤御伤魂 提交于 2019-11-29 02:08:25

You imported sqrt into your module, just apply the decorator there in your own global namespace:

sqrt = print_args_decor(sqrt)

This sets the name sqrt in your module namespace to the result of the decorator. There is no requirement that sqrt was originally defined in this module.

It is up to the decorator to uses the functools.wraps() decorator to preserve function metadata such as the name and docstring.

Decorating a class is no different in this respect:

ClassName = decorator(ClassName)

On Python 2, for methods you need to be careful to grab the original unbound function; easiest is to use the method.__func__ attribute:

try:
    # Python 2
    ClassName.function_name = decorator(ClassName.function_name.__func__)
except AttributeError:
    # Python 3
    ClassName.function_name = decorator(ClassName.function_name)

I've wrapped the above in a try...except to make the pattern work across Python versions. The alternative is to grab the function object out of the class __dict__ to avoid the descriptor protocol from kicking in:

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