# Chinese每次创建由原来的type改为Mymeta,因此可以在Mymeta内对类进行一些特殊定制
# 示例一:类名的大小写强控
#
# class Mymeta(type):
# def __init__(self,class_name,class_bases,class_dic):
# if not class_name.istitle():
# raise TypeError('类名的首字母必须大写!')
# super(Mymeta,self).__init__(class_name,class_bases,class_dic)
# class Chinese(object,metaclass=Mymeta):
#
# country = 'China'
#
# def __init__(self,name,age):
# self.name = name
# self.age = age
#
# def talk(self):
# print('%s is talking' % self.name)
# 示例二:注释强控
class Mymeta(type):
def __init__(self,class_name,class_bases,class_dic):
if '__doc__' not in class_dic or not class_dic['__doc__'].strip():
raise TypeError('必须有注释,且注释不能为空!')
super(Mymeta,self).__init__(class_name,class_bases,class_dic)
class Foo(object,metaclass=Mymeta):
'''
注释
'''
pass