Python之面向对象元类

时间:2021-02-18 03:47:38

Python之面向对象元类

  call方法:

 class People:
def __init__(self,name):
self.name=name
#
def __call__(self, *args, **kwargs):
print('call')
# p=People('George')
print(callable(People))
print(callable(p)) p()

  元类:  

 class People:
def __init__(self,name):
self.name=name p=People('egon') # print(type(p))
#
# print(type(People)) #typer--->类------>对象 class Foo:
x=1
def run(self):
pass
print(type(Foo)) #type成为元类,是所有类的类,利用type模拟class关键字的创建类的过程
def run(self):
print('%s is runing' %self.name) class_name='Bar'
bases=(object,)
class_dic={
'x':1,
'run':run
} Bar=type(class_name,bases,class_dic)
print(Bar)
print(type(Bar))

  自定义元类:

 # class Foo(metaclass=type):
# x=1
# def run(self):
# print('running')
#
#
# # type('Foo',(object,),{'x':1,'run':run}) # class Mymeta(type):
# def __init__(self,class_name,class_bases,class_dic):
# # print(self)
# # print(class_name)
# # print(class_bases)
# # print(class_dic)
# for key in class_dic:
# if not callable(class_dic[key]):continue
# if not class_dic[key].__doc__:
# raise TypeError('小子,你没写注释,赶紧去写')
#
# # type.__init__(self,class_name,class_bases,class_dic)
# class Foo(metaclass=Mymeta):
# x=1
# def run(self):
# 'run function'
# print('running') # Foo=Mymeta('Foo',(object,),{'x':1,'run':run}) # print(Foo.__dict__) class Mymeta(type):
def __init__(self,class_name,class_bases,class_dic):
pass
def __call__(self, *args, **kwargs):
# print(self)
obj=self.__new__(self)
self.__init__(obj,*args,**kwargs) #obj.name='egon'
return obj
class Foo(metaclass=Mymeta):
x=1
def __init__(self,name):
self.name=name #obj.name='egon'
def run(self):
'run function'
print('running')
# print(Foo.__dict__) f=Foo('egon') print(f) print(f.name)

友情学习链接