ref: http://mp.weixin.qq.com/s?__biz=MjM5NzU0MzU0Nw==&mid=400694133&idx=1&sn=9375ff6a0d3552e09e0405c8a8d88289&scene=0#wechat_redirect
#!/usr/bin/env python # -*- coding:‘uft-8‘ -*- class Student(object): def __init__(self,name=None,gender=None): self.name,self.gender=name,gender def __format__(self,pattern): return ‘{}(name={name!r},gender={gender!r})‘.format(self.__class__.__name__, **self.__dict__) class Class_(object): def __init__(self,*students): self.__student=students def __format__(self,pattern): return ‘{}({})‘.format(Class_.__name__, ‘,‘.join(format(_,pattern) for _ in self.__student)) s1=Student(‘Jan‘,‘Fmale‘) s2=Student(‘Mike‘,‘male‘) a_instance_class=Class_(s1,s2) print format(a_instance_class,‘‘) a_instance_class1=eval(format(a_instance_class,‘‘))
output:
Class_(Student(name=‘Jan‘,gender=‘Fmale‘),Student(name=‘Mike‘,gender=‘male‘))
时间: 2024-12-29 17:18:38