Have you tried playing around with the setattr and getattr functions? With these you can write and read the writable attributes of the object directly.
So you could do something like this:
map_of_students = map(lambda student: setattr(student, "age", getattr(student, "age") + 3), students)
print("Age BEFORE list: " + str(students[0].age)); list(map_of_students); print("Age AFTER list: " + str(students[0].age))
In this case the original students' list will be updated with the new ages for each student, which might not be necessarily what you want, but can easily be worked around by making a backup of the original list's objects before casting the map object to a list. In that case, you could do:
students_bkp = []
for s in students:
students_bkp.append(Student(**s.__dict__))
Here is the full snippet:
class Student(object):
def __init__(self,name,age):
self.name = name
self.age = age
student1 = Student('StudOne',18)
student2 = Student('StudTwo',20)
student3 = Student('StudThree',29)
students = [student1,student2,student3]
students_bkp = []
for s in students:
students_bkp.append(Student(**s.__dict__))
map_of_students = map(lambda student: setattr(student, "age", getattr(student, "age") + 3), students)
print("Age BEFORE list: " + str(students[0].age)); list(map_of_students); print("Age AFTER list: " + str(students[0].age)); print("Age BKP: " + str(students_bkp[0].age))
increased_students_age_listhas exactly the same contents asstudents. You have two lists that are identical, there's no point in that.