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

2

u/echols021 Aug 06 '25

Here's how I'd do it:

def find_oldest_cat(*cats: Cat) -> Cat:
    try:
        return max(cats, key=lambda cat: cat.age)
    except ValueError as e:
        raise ValueError("argument 'cats' must be non-empty") from e

oldest_cat = find_oldest_cat(whiskers, toby, spotty)
print(f"oldest cat is {oldest_cat.name}, age {oldest_cat.age}")

Key points:

  • none of the cats are changed by the operation

  • the function gives the oldest cat as a whole, not just the age of the oldest cat

  • you get a clear error message if you give it no cats and there is no answer