Skip to content

Week 9: Objects and classes

~9 min

This week's goal

This week you group data and the functions that work on it into classes. You learn to create objects, to protect internal values with properties, to build a subclass with super, and to make your objects work with len, print, and == by defining special methods. By the end you can write an Account class with a subclass and check it with tests.

The main text is Part 7 of the Python course, lessons 1 to 5.

Day-by-day (about 1.5-2 hours per day)

Day 1. Read classes and objects. A class is a blueprint and an object is one instance made from it. Write a small Book class with a title and an author, set in __init__, and make two books. Print the attributes of each one. Notice that the two objects keep their own values.

Day 2. Read encapsulation and properties. Add a balance attribute to a new Account class. Use a property so that a negative deposit raises a ValueError. Test the property with a small assert in the file and with one try block that shows the error message.

Day 3. Read inheritance and super. Create a SavingsAccount class that inherits from Account and adds an interest method. Use super().__init__ so that the parent sets up the balance. Check that a savings account is also an Account with isinstance.

Day 4. Read polymorphism and protocols. Write a function that takes a list of accounts of different kinds and prints each one's balance by calling the same method on all of them. This is polymorphism in practice: the function does not need to know which kind of account it has.

Day 5. Read dunder methods. The Python data model reference lists the special methods. Give Account a __str__ method so that print(account) shows a readable line, and an __eq__ method so that two accounts with the same owner and balance compare equal. Test both.

Day 6. Put it together. Write a file accounts.py with the Account and SavingsAccount classes. Add a tests section at the bottom with at least six assert statements that check deposits, withdrawals, the error for a negative amount, interest, the string form, and equality. Run the file. It should print nothing when all the tests pass.

Day 7 is a rest day.

Deliverable

accounts.py with an Account class and a SavingsAccount subclass. The file defines a property for the balance, __str__, and __eq__. Its test section runs at least six assert checks, and running the file prints nothing when all of them pass.

Check yourself

  1. What is the difference between a class and an object?
  2. What does super().__init__(...) do inside a subclass?
  3. Why use a property instead of a plain attribute when a value must stay valid?
  4. What does print(account) call, and which method controls its output?
  5. Why does polymorphism let one function handle several kinds of objects?
Answers
  1. A class is the definition, which says what data and methods each object has. An object is one instance created from the class, with its own values.
  2. It calls the parent class's __init__ so that the parent sets up its attributes. The subclass then adds its own.
  3. A property runs code when the value is read or set. You can check the value and reject a bad one, which a plain attribute cannot do.
  4. It calls __str__ on the object, and __str__ returns the text that print shows. If it is not defined, Python uses a default representation.
  5. Each class can provide its own version of the same method. The function calls the method and gets the right behavior for each kind, without checking the type first.

Sources