开发手册 欢迎您!
软件开发者资料库

Python 方法函数使用多个装饰器及示例代码

本文主要介绍Python中,单个方法函数使用多个装饰器(@property)的方法,以及相关的示例代码。

1、自定义装饰器使用写法及示例代码

文档Python 自定义装饰器使用写法及示例代码

2、一个函数方法使用两个装饰器

文档https://docs.python.org/3/reference/compound_stmts.html#function

from functools import wrapsdef makebold(fn):    @wraps(fn)    def wrapped(*args, **kwargs):        return "" + fn(*args, **kwargs) + ""    return wrappeddef makeitalic(fn):    @wraps(fn)    def wrapped(*args, **kwargs):        return "" + fn(*args, **kwargs) + ""    return wrapped@makebold@makeitalicdef hello():    return "hello world"@makebold@makeitalicdef log(s):    return sprint hello()        # returns "hello world"print hello.__name__ # with functools.wraps() this returns "hello"print log('hello')   # returns "hello"

相关文档:

Python内置装饰器(@property、@staticmethod、@classmethod)使用及示例代码

Python中@staticmethod和@classmethod区别及使用示例代码