__str__

When you pass an object to print, Python asks the object for some text to show. A class can give that text with a method named __str__.

class Book:
    def __init__(self, title, pages):
        self.title = title
        self.pages = pages

    def __str__(self):
        return f"{self.title} ({self.pages} pages)"

book = Book("Dune", 412)
print(book)

Output:

Dune (412 pages)

Without __str__, printing the same object gives Python's default text:

<__main__.Book object at 0x12d2aa7ee5a0>

This shows only the class name and a memory address. The address changes each time you run the program. It tells the reader nothing about the book.

Names like __init__ and __str__ have two underscores at each end. They are called dunder methods, which is short for "double underscore". You don't call them by name yourself. Python calls them for you at the right moment.

In the exercise, write a __str__ method for Employee.