# metaclass 自定义元类
class Human(type):
@staticmethod
def __new__(mcs, *args, **kwargs):
class_ = super().__new__(mcs,*args)
if kwargs:
for name,value in kwargs.items():
setattr(class_,name,value) # 设置类属性
return class_
class Student(object,metaclass=Human,country="China",freedom=True):
pass
print(Student.country)
print(Student.freedom)
python 自定义元类
最新推荐文章于 2024-07-12 16:16:27 发布