206-魔术方法:__dict__()
__dict__()
方法的作用:
- 把对象转化为字典
- 把对象的属性转化为字典的键和值
例如,把学生信息的对象转成列表后保存成列表再保存到文件
from Student import Student
students = []
s1 = Student('ZhangSan', 23, '10086')
s2 = Student('LiSi', 24, '10010')
students.append(s1)
students.append(s2)
students = [i.__dict__ for i in students]
print(students)