2019年8月13日 继承的方式完成包装

RT

class Foo:
    def __init__(self,name):
        self.name=name

    def __getattr__(self, item):
        print('你找的属性%s不存在'%item)
    def __setattr__(self, key, value):
        print('执行setattr',key,value)
        if type(value) is str:#判断是否为str类型
            print('开始设置')
            self.__dict__[key] = value
        else:
            print('必须是str类型')
    def __delattr__(self, item):
        print('不允许删除属性%s'%item)
        # self.__dict__.pop(item)#底层删除


f1=Foo('abc')
f1.age=18
del f1.name
print(f1.__dict__)

二次加工标准类型(包装)

 》》》》》》》》》》》》》》》》》》》》》》》》》

 注意需要传递自己self,所以可用super方法

原文地址:https://www.cnblogs.com/python1988/p/11348972.html