有关元类是什么大家自己搜索了解,我这里写一下实现元类的两种写法
# 自定义元类 #继承type class LowercaseMeta(type): ‘‘‘ 修改类的属性名称为小写的元类 ‘‘‘ # __new__ 类的实列的创建 #__init__类的初始化 #方法一: def __new__(mcs,name,bases,attrs): lower_attrs={} for k,v in attrs.items(): if not k.startswith(‘__‘): #排除magic method lower_attrs[k.lower()]=v else: lower_attrs[k]=v return type.__new__(mcs,name,bases,lower_attrs) # 方法二: # def __new__(cls, *args, **kwargs): # #将要替换args # new_args=[] # for arg in args: # if isinstance(arg,dict): # low_attrs = {} # for k,v in arg.items(): # if str(k).startswith(‘__‘): #magic method # low_attrs[k]=v # else: #排除magic method # low_attrs[str(k).lower()]=v # new_args.append(low_attrs) # else: # new_args.append(arg) # return type.__new__(cls,*new_args,**kwargs) #类 class LowercaseClass(metaclass=LowercaseMeta): #py3 BAR=True def HELLO(self): print(‘hello‘) #此时BAT,HELLO 都变为小写 print(dir(LowercaseClass)) LowercaseClass().hello()
两种方法的执行结果相同:
原文:https://www.cnblogs.com/feiquan/p/11110632.html