有了元类之后的属性查找
类的属性查找顺序:先从类本身中找--->mro继承关系去父类中找--->去自己定义的元类中找--->type中--->报错
对象的属性查找顺序:先从对象自身找--->类中找--->mro继承关系去父类中找--->报错
举例
class Mymeta(type):
n=444
def __call__(self,*args,**kwargs):
obj=self.__new__(self)
obj.__init__(*args,**kwargs)
return obj
class Bar(object):
# n=333
pass
class Foo(Bar):
# n=222
pass
class OldboyTeacher(Foo,metaclass=Mymeta):
# n=111
school='oldboy'
def __init__(self,name,age):
self.name=name
self.age=age
def say(self):
print('%s says welcome to the oldboy to learn Python' % self.name)
o=OldboyTeacher('egon',18) #触发OldboyTeacher的类中的__call__方法的执行,进而执行self.__new__开始查找
print(OldboyTeacher.n)