Python自定义类装饰器

from functools import wraps


class remarkable:
    """类装饰器"""

    def __init__(self, blanklines=0, ps="这是一个带参数示例的装饰器"):
        self.blanklines = blanklines
        self.ps = ps

    def __call__(self, func):
        @wraps(func)
        def inner(*args, **kwargs):
            print("\n" * self.blanklines)
            func(*args, **kwargs)
            print("\n" * self.blanklines)
            print(self.ps)

        return inner


@remarkable(blanklines=2, ps="这是一个带参数示例的装饰器")
def sayHello(name, *args, boss=None, **kwargs):
    print("{}:HELLO WORLD.{}-{}-{}".format(name, args, boss, kwargs))


sayHello("张三", "李四", "王麻子",  boss="洪七公", father="鱼")

Python自定义类装饰器_第1张图片

 

你可能感兴趣的:(python,开发语言)