example09.py 373 B

12345678910111213141516171819
  1. import copy
  2. class PrototypeMeta(type):
  3. def __init__(cls, *args, **kwargs):
  4. super().__init__(*args, **kwargs)
  5. cls.clone = lambda self, is_deep=True: \
  6. copy.deepcopy(self) if is_deep else copy.copy(self)
  7. class Student(metaclass=PrototypeMeta):
  8. pass
  9. stu1 = Student()
  10. stu2 = stu1.clone()
  11. print(stu1 == stu2)
  12. print(id(stu1), id(stu2))