Classes and OOP in Python
โ Report an issue with this lessonclass Course:
def __init__(self, title, price):
self.title = title
self.price = price
def __repr__(self):
return f"Course({self.title!r}, {self.price})"
def __eq__(self, other):
return self.title == other.title
Dunder methods (__repr__, __eq__,
__len__) let your objects work naturally with Python's
built-ins โ print(), ==, len() โ
instead of needing special-case handling everywhere they're used.
Try it yourself
Exercise: Add __repr__ and __eq__ to Course so print() and == work naturally.
Expected output: open-ended โ there's no single correct output here, just get your code running without errors.
Run your code and get it working before marking this lesson complete.
// free preview
9 more lessons โ including Capstone project: a tested, packaged CLI โ plus a certificate are waiting.
Unlock the full course โ $149.99