class Person:
def __init__(self, age):
self._age = age
@property
def age(self):
return self._age
persons = [Person(20), Person(30), Person(19), Person(17), Person(15), Person(25)]
result = persons[0]
for person in persons:
if person.age < result.age:
result = person
Can someone please explain to me, what happens in this for loop? As far as I understand it checks, if the input of age is smaller than the result and if so, it "puts" it into the result variable. The solution states 15 but how do we get there?
min(persons, key=attrgetter('age'))resultis just a name for an object. The nameresultis assigned to thePersonobject with the lowestageattribute.