python元类深入理解
Posted jiaojianglong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python元类深入理解相关的知识,希望对你有一定的参考价值。
class A(): def __init__(self,name): self.name = name print("创建了一个实例") a = type("a",(A,),"name":"jiao") print(a) #<class ‘__main__.a‘> print(a.name) #jiao print(a("jiang")) #创建了一个实例 #<__main__.a object at 0x00000280A973AA58>
class UpperAttrMetaClass(type): def __new__(cls,class_name,class_parents,class_attr, *args, **kwargs): print("__new__") class_attr[‘name‘] = "jiao" return type.__new__(cls,class_name,class_parents,class_attr) def __init__(self,*args,**kwargs): print("__init__") super().__init__(*args, **kwargs) self.__cache = def __call__(self, *args, **kwargs): print("__call__") if args in self.__cache: return self.__cache[args] else: obj = super().__call__(*args) self.__cache[args] = obj return obj class A(metaclass=UpperAttrMetaClass): def __init__(self,name): self.name = name print("a.__init__")
class Singleton(type): def __init__(cls,*args,**kwargs): cls.__instance = None super().__init__(*args,**kwargs) def __call__(cls, *args, **kwargs): if cls.__instance is None: cls.__instance = super().__call__(*args,**kwargs) return cls.__instance else: return cls.__instance class Spam(metaclass=Singleton): def __init__(self): print("Creating Spam")
2.缓存模式
import weakref class Cached(type): def __init__(cls,*args,**kwargs): super().__init__(*args,**kwargs) cls.__cache = weakref.WeakValueDictionary() def __call__(cls, *args, **kwargs): if args in cls.__cache: return cls.__cache[args] else: obj = super().__call__(*args) cls.__cache[args] = obj return obj class Spams(metaclass=Cached): def __init__(self,name): print("Creating Spam(!r)".format(name)) self.name = name
from collections import OrderedDict class Typed: _excepted_type = type(None) def __init__(self,name=None): self._name = name def __set__(self, instance, value): if not isinstance(value,self._excepted_type): raise TypeError("Excepted"+str(self._excepted_type)) instance.__dict__[self._name] = value class Integer(Typed): _excepted_type = int class Float(Typed): _excepted_type = float class String(Typed): _excepted_type = str class OrderedMeta(type): def __new__(cls, clsname,bases,clsdict): d = dict(clsdict) order = [] for name,value in clsdict.items(): if isinstance(value,Typed): value._name = name order.append(name) d[‘_order‘] = order return type.__new__(cls,clsname,bases,d) @classmethod def __prepare__(metacls, name, bases): return OrderedDict() #注:__prepare__该方法会在类定义一开始的时候调用,调用时以类名和基类名称作为参数,它必须返回一个映射对象,供处理类定义体时调用 #eg. class Structure(metaclass=OrderedMeta): def as_csv(self): return ‘,‘.join(str(getattr(self,name)) for name in self._order) class Stock(metaclass=OrderedMeta): name = String() shares = Integer() price = Float() def __init__(self,name,shares,price): self.name = name self.shares = shares self.price = price s = Stock("haha",23,23.3) print(s.name) s = Stock(34,23,34) # print(s.as_csv())
以上是关于python元类深入理解的主要内容,如果未能解决你的问题,请参考以下文章