1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| class Mymeta(type): def __init__(self,class_name,class_bases,class_dic): if not class_name.istitle(): raise TypeError('类名的首字母必须大写')
if '__doc__' not in class_dic or not class_dic['__doc__'].strip(): raise TypeError('必须有注释,且注释不能为空')
super(Mymeta,self).__init__(class_name,class_bases,class_dic)
def __call__(self, *args, **kwargs): #obj=Chinese('hjx',age=18) # print(self) #self=Chinese # print(args) #args=('hjx',) # print(kwargs) #kwargs={'age': 18}
#第一件事:先造一个空对象obj obj=object.__new__(self) #第二件事:初始化obj self.__init__(obj,*args,**kwargs) #第三件事:返回obj return obj
class Chinese(object,metaclass=Mymeta): ''' 中文人的类 ''' country='China'
def __init__(self,namem,age): self.name=namem self.age=age
def talk(self): print('%s is talking' %self.name)
obj=Chinese('egon',age=18) #Chinese.__call__(Chinese,'hjx',18)
print(obj.__dict__)
|