十九.面向对装饰器

一  装饰器

def decorate(cls):
    print('类的装饰器开始运行啦------>')
    return cls

@decorate #无参:People=decorate(People)
class People:
    def __init__(self,name,age,salary):
        self.name=name
        self.age=age
        self.salary=salary

p1=People('egon',18,3333.3)

类的装饰器:无参
def Super(**kwargs):
    def inner(obj):
        for key,val in  kwargs.items():
            print(key,val,"这是传过来的参数")
            setattr(obj,key,val)
        return  obj
    return  inner


@Super(a=1,b=2)            #  Foo=super(Foo)
class Foo(object):
    pass
print(Foo.__dict__)

# a 1 这是传过来的参数
# b 2 这是传过来的参数
# {'__module__': '__main__', '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None, 'a': 1, 'b': 2}
def typeassert(**kwargs):
    def decorate(cls):
        print('类的装饰器开始运行啦------>',kwargs)
        return cls
    return decorate
@typeassert(name=str,age=int,salary=float) #有参:1.运行typeassert(...)返回结果是decorate,此时参数都传给kwargs 2.People=decorate(People)
class People:
    def __init__(self,name,age,salary):
        self.name=name
        self.age=age
        self.salary=salary

p1=People('egon',18,3333.3)

# 类的装饰器开始运行啦------> {'name': <class 'str'>, 'age': <class 'int'>, 'salary': <class 'float'>}
from functools import wraps
def wrapper(func):
    @wraps(func)
    def inner(self,*args,**kwargs):
        # 此时的self是Person的实例对象
        self.name += "爱吃糖11111111111111111111111"
        # func ===>  printInfo
        ret = func(self,*args,**kwargs)
        return ret
    return inner

class Person(object):
    def __init__(self,name):
        self.name = name
    @wrapper     #printInfo=wrapper(printInfo)
    def printInfo(self):
        print(self.name)

# printInfo ==> wraper(printInfo) ==> inner
aa=Person("张三")
aa.printInfo()
# 张三爱吃糖11111111111111111111111

装饰器描述符来实现 实例化参数类型判断

class Typed:
    def __init__(self,name,expected_type):
        self.name=name
        self.expected_type=expected_type
    def __get__(self, instance, owner):
        if instance is None:
            return self
        return instance.__dict__[self.name]

    def __set__(self, instance, value):
        if not isinstance(value,self.expected_type):
            raise TypeError('Expected %s' %str(self.expected_type))
        instance.__dict__[self.name]=value

    def __delete__(self, instance):
        instance.__dict__.pop(self.name)

def typeassert(**kwargs):
    def decorate(cls):
        for name,expected_type in kwargs.items():
            setattr(cls,name,Typed(name,expected_type))
        return cls
    return decorate
@typeassert(name=str,age=int,salary=float) #有参:1.运行typeassert(...)返回结果是decorate,此时参数都传给kwargs 2.People=decorate(People)
class People:
    def __init__(self,name,age,salary):
        self.name=name
        self.age=age
        self.salary=salary

print(People.__dict__)
p1=People('egon',18,3333.3)
def fun(obj):
    obj.x=1
    obj.y= 2888
    return  obj
@fun  #  ====>>> Foo=fun(Foo)
class Foo(object):
    pass
print(Foo.__dict__)

二、类装饰器实现单例模式

def wrapper(obj):
    def inner(name,*args,**kwrags):
        if not obj.instance:
            obj.instance = obj(name)
        return obj.instance
    return inner

@wrapper                          >>>>> Person=wrapper(Person)
class Person(object):
    instance = None
    def __init__(self,name):
        self.name = name

    def printInfo(self):
        print(self.name)

zs = Person('张三')
zs.printInfo()
print(id(zs))

ls = Person('李四')
ls.printInfo()
print(id(ls))



张三
2514106528432
张三
2514106528432

 

转载于:https://www.cnblogs.com/Sup-to/p/11090017.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值