继承的方式完成包装__attr__

__getattr__    当实例要调用的属性不存在的时候会触发

__setattr__    当实例要设置属性的时候会触发

__delattr__    当实例要删除属性的时候会触发

这三个方法是python内置的方法,如果不自己定义的话,就是默认的,自己定义的话就是用自己定义的

1、__getattr__           当要调用的属性不存在的时候会触发

没有自己定义__getattr__的时候

class Foo:
    def __init__(self,name):
        self.name=name
f1=Foo("gouguoqi")
print(f1.name)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

gouguoqi

我们自己定义一个__getattr__

class Foo:
    def __init__(self,name):
        self.name=name
    def __getattr__(self, item):
        print("你要找的属性%s不存在" %item)
f1=Foo("gouguoqi")
print(f1.name)
print(f1.age)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

gouguoqi

你要找的属性age不存在

2、__setaddr__    给属性设置值的时候会触发

class Foo:
    def __init__(self,name):
        self.name=name
    def __setattr__(self, k, v):
        print("执行setattr")
f1=Foo("gouguoqi")#实例化的过程就是运行init,init就有一个赋值name=gouguoqi所以就触发setaddr

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

执行setattr

2.1我们在没有自己定义__setattr__的时候,系统是帮我们把属性设置到属性字典里面

class Foo:
    def __init__(self,name):
        self.name=name
f1=Foo("gouguoqi")#实例化的过程就是运行init,init就有一个赋值name=gouguoqi所以就触发setaddr
print(f1.__dict__)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

{'name': 'gouguoqi'}

2.2但是我们自己定制的那个setattr只是执行了一个打印的操作,并没有帮我们把值新增到属性字典里面去

class Foo:
    def __init__(self,name):
        self.name=name
    def __setattr__(self, k, v):
        print("执行setattr")
f1=Foo("gouguoqi")#实例化的过程就是运行init,init就有一个赋值name=gouguoqi所以就触发setaddr
f1.age=28
f1.gender="male"
print(f1.__dict__)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

执行setattr

执行setattr

执行setattr

{} 

2.3现在有一个需求,传入的值必须是字符串,应该怎么实现

先完成以下判断逻辑,然后在具体的去实现功能

class Foo:
    def __init__(self,name):
        self.name=name
    def __setattr__(self, k, v):
        print("执行setattr")
        if type(v) is str:
            print("开始设置")
        else:
            print("必须是字符串")
f1=Foo("gouguoqi")
f1.age=28
print(f1.__dict__)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

执行setattr

开始设置

执行setattr

必须是字符串

{}

实现设置功能,只能是字符串才能设置进去

class Foo:
    def __init__(self,name):
        self.name=name
    def __setattr__(self, k, v):
        print("执行setattr")
        if type(v) is str:
            print("开始设置")
            self.__dict__[k]=v
        else:
            print("必须是字符串")
f1=Foo("gouguoqi")
f1.age=28
print(f1.__dict__)

 

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

执行setattr

开始设置

执行setattr

必须是字符串

{'name': 'gouguoqi'}

2.4还可以实现。如果是字符串的话,把字符串变为大写

class Foo:
    def __init__(self,name):
        self.name=name
    def __setattr__(self, k, v):
        print("执行setattr")
        if type(v) is str:
            print("开始设置")
            self.__dict__[k]=v.upper()
        else:
            print("必须是字符串")
f1=Foo("gouguoqi")
f1.age=28
print(f1.__dict__)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

执行setattr

开始设置

执行setattr

必须是字符串

{'name': 'GOUGUOQI'}

3、__delattr__  删除属性

class Foo:
    def __init__(self,name):
        self.name=name
    def __delattr__(self, item):
        print("执行delattr",item)
        self.__dict__.pop(item)#操作底层的属性字典的key
f1=Foo("gouguoqi")
del f1.name#把f1传给self,把name传给item
print(f1.__dict__)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

执行delattr name

{}

3.1那么系统默认的删除就是这样的啊,我们为什么要自己定义一个这个呢,那么又来一个新的需求,所有的属性都不能删除,改怎么做呢

class Foo:
    def __init__(self,name):
        self.name=name
    def __delattr__(self, item):
        print("%s属性不允许删除" %item)
f1=Foo("gouguoqi")
del f1.name
print(f1.__dict__)

C:\python35\python3.exe D:/pyproject/day26/双下划线开头的attr方法.py

name属性不允许删除

{'name': 'gouguoqi'

4、包装 

class List(list):#定义个类,继承系统的list
    pass

l2=list("hello world")
print(l2,type(l2))

l1=List("hello world")
print(l1,type(l1))

C:\python35\python3.exe D:/pyproject/day26/包装标准类型.py

['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] <class 'list'>

['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] <class '__main__.List'>

4.1现在想要实现的是 定制一个方法,显示出列表中间元素的那个元素内容

class List(list):#定义个类,继承系统的list
    def show_middle(self):
        mid_index=int(len(self)/2)#取到中间的索引
        return self[mid_index]#返回列表的中间的索引的那个值
l1=List("helloworld")
print(l1,type(l1))
print(l1.show_middle())

C:\python35\python3.exe D:/pyproject/day26/包装标准类型.py

['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'] <class '__main__.List'>

w

4.2我们自己定义一个apped的方法

结果就是append这个方法不能追加了。因为自己定义的是pass

class List(list):#定义个类,继承系统的list
    def append(self, object):
        pass
    def show_middle(self):
        mid_index=int(len(self)/2)#取到中间的索引
        return self[mid_index]#返回列表的中间的索引的那个值
l1=List("helloworld")
l1.append(111)
print(l1)

C:\python35\python3.exe D:/pyproject/day26/包装标准类型.py

['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']

实现真正的append操作

class List(list):#定义个类,继承系统的list
    def append(self, object):
        if type(object) is str:
            list.append(self,object)#用父类的append方法
    def show_middle(self):
        mid_index=int(len(self)/2)#取到中间的索引
        return self[mid_index]#返回列表的中间的索引的那个值
l1=List("helloworld")
l1.append("111")
print(l1)

C:\python35\python3.exe D:/pyproject/day26/包装标准类型.py

['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '111']

类调用方法,推荐用super

class List(list):#定义个类,继承系统的list
    def append(self, object):
        if type(object) is str:
            # list.append(self,object)#用父类的append方法
            super().append(object)#super就是调用父类的方法,不用传self
    def show_middle(self):
        mid_index=int(len(self)/2)#取到中间的索引
        return self[mid_index]#返回列表的中间的索引的那个值
l1=List("helloworld")
l1.append("222")
print(l1)

C:\python35\python3.exe D:/pyproject/day26/包装标准类型.py

['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '222']

4.3这样就实现了  append我们自己定制了一个,只能添加字符串,不能append数字

class List(list):#定义个类,继承系统的list
    def append(self, object):
        if type(object) is str:
            # list.append(self,object)#用父类的append方法
            super().append(object)#super就是调用父类的方法,不用传self
        else:
            print("只能append字符串")
    def show_middle(self):
        mid_index=int(len(self)/2)#取到中间的索引
        return self[mid_index]#返回列表的中间的索引的那个值
l1=List("helloworld")
l1.append("222")
print(l1)
l1.append(666)

C:\python35\python3.exe D:/pyproject/day26/包装标准类型.py

['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '222']

只能append字符串

转载于:https://www.cnblogs.com/gouguoqilinux/p/9215536.html

import inspect import types class KeyWordManage: _GLOBAL_ALIAS_MAP = {} # 全局别名注册表 @staticmethod def chinese_alias(*names: str): """ 装饰器,用于为函数添加中文别名 1. 同时支持类方法和普通函数 2. 支持多中文别名 Args: names (str): 中文别名, 可以有多个 """ def decorator(func): # 判断方法是否有_chinese_aliases属性 if not hasattr(func, '_chinese_aliases'): print(f"别名集合设置:{names}") # 如果方法没有_chinese_aliases属性,则创建一个空的集合 func._chinese_aliases = set() # 将中文别名添加到方法属性的集合中 func._chinese_aliases.update(names) # 返回装饰器函数 return func return decorator @staticmethod def chinese_alias_of_function(*names: str): """ 装饰器,用于为函数添加中文别名 """ def decorator(func): for name in names: if name not in KeyWordManage._GLOBAL_ALIAS_MAP: print(f"普通函数加入中文别名:{name}") KeyWordManage._GLOBAL_ALIAS_MAP[name] = func # 返回装饰器函数 return func return decorator class ChineseAliasMeta(type): """ 自定义的元类,用于在创建类的过程中添加中文别名 1.自动完成别名注册,无需手动调用add_chinese_aliases 2.继承体系下所有子类自动获得该能力 """ def __call__(self, *args, **kwargs): """ 创建新的类实例 不使用new是因为多继承时,会导致重复调用new方法,导致反复覆盖,影响效率 """ # 创建新的类实例, klass 是创建的类实例, 例如:<class '__main__.MyClass'> instance = super().__call__(*args, **kwargs) # 遍历实例的属性,查找具有 _chinese_aliases 属性的方法,并将别名添加到实例 for attr_name in dir(instance): # dir(klass) 返回类的所有属性,例如:['__init__', 'add_chinese_aliases', 'chinese_alias'] method = getattr(instance, attr_name) # 获取属性 if callable(method) and hasattr(method, '_chinese_aliases'): # 判断方法是否可调用且具有chinese_name属性 for alias in method._chinese_aliases: # 将方法添加到类属性中 setattr(instance, alias, method) for name, func in KeyWordManage._GLOBAL_ALIAS_MAP.items(): # 如果方法不是类方法 # print("映射表", KeyWordManage._GLOBAL_ALIAS_MAP) # print(f"动态设置中文别名: {name}") if inspect.isfunction(func) and not inspect.ismethod(func): # 对于普通函数,使用静态方法包装 setattr(instance, name, staticmethod(func)) else: setattr(instance, name, func) # 返回类实例 return instance @KeyWordManage.chinese_alias_of_function("加法1") def add1(a, b): return a + b # 使用示例2 # @add_chinese_aliases class Calculator2(metaclass=ChineseAliasMeta): @KeyWordManage.chinese_alias("加法") def add(self, a, b): return a + b + 1 class Calculator21(Calculator2): @KeyWordManage.chinese_alias("减法") def sub(self, a, b): return a - b if __name__ == '__main__': # 测试 calc = Calculator21() print(calc.加法1(2, 3)) # 调用不存在的方法,但是被关键字注册了。 print(calc.add(2, 3)) # 输出: 5 print(calc.加法(2, 3)) # 输出: 5(通过中文别名调用) print(calc.sub(2, 3)) # 输出: -1 print(calc.减法(2, 3)) # 输出: -1(通过中文别名调用) 优化代码
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值