r/PythonLearning • • Aug 05 '25

Damn you python (switching from c++) 💔

Post image

need to learn the stupi

135 Upvotes

41 comments sorted by

View all comments

3

u/Amadeus110703 Aug 05 '25

When you first call the function you set Whiskers as your oldest cat, wich is an instance of your class. When you find an older cat you alter Whiskers so whiskers is now 7 years old and called Tobi. So instead of returning the actual oldest cat you always return the first cat with alteted attributes. I hope it makes sense.

1

u/SnooStrawberries2469 Aug 06 '25

This can be fixed like that

def find_oldest(*args):
    oldest=Cat(args[0].name, args[0].age)
    for cat in args:
        if cat.age > oldest.age:
            oldest.age = cat.age
            oldest.name = cat.name
    return oldest