1、遍历list1这个列表
2、找到list1内的对应索引
3、通过__dict__来获取类属性,且赋值给a
4、用字典取值的方式来获取最终值
class A(object):
def __init__(self, name, age):
self.name = name
self.age = age
list1 = [
A("小明", 15),
A("小红", 13),
A("小砍", 15),
A("小次", 16)
]
for i in range(len(list1)):
a = list1[i].__dict__
print(a.get("name"), a.get("age"))