Lesson 10
I am Your Father
Chapter 1: Introduction
Recap
Welcome to the final lesson in the ‘Core’ series!
As usual, let us reflect on how we got here.
The basics
You started out from the very basics by learning to design and program a guessing game. You then spent the next few lessons diving into the details of all the basic building blocks as used in Python. You then started writing self-explanatory and modular codes with functions.
Complex data structures
You are then introduced to complicated Python data structures like list
s, range
, tuple
s, dict
s, and set
s; along with useful iterators like enumerate
and zip
in the last lesson.
Object-oriented programming
The concept of object oriented programming emerged from Lesson 7 onwards. I introduced the idea of objects having states and properties, and objects having actions/methods associated with them. Putting the two ideas together, you wrote custom classes with member attributes and methods.
In the previous lesson, I focussed on the interaction between different objects of different classes. You saw that an object can receive another object as input argument to its methods. An object can then invoke the other object’s (public) methods to get it to do things. In this case, there is a dependency between the two object classes.
We can also have a more specific type of dependency called association, which is where an object ‘has’ another object as its attribute (Road
has Car
s). The degree of association can be even stronger with aggregation where an object is part of another (a Song
is part of a Playlist
), and composition where an object is exclusively part of another object (a Room
only exist as part of a House
). For practical purposes, there is no need to stress about the minute distinction between these three. It is sufficient for you to know that you can assign an object as an instance attribute of a class, and later use these objects in the instance methods for the class.
You have also attempted to continue refactoring your robot project to make it even more object-oriented. You created a RobotFactory
class to build your robots, and later created more classes like Drink
and Grid
for your robot to interact with.
Python specific topics
We also looked at some Python specific topics. We briefly discussed list comprehension and how it can make your code more compact, readable, and possibly even faster.
You also learnt what JSON files are and how to load objects into Python from JSON, and how to write Python objects into JSON files for storage.
We also extended the idea of Python modules further, by organising them into a package. Recall that a package is a collection of related modules organised in a nice hierarchy.
Finally we quickly looked at the fact that Python Exception
s are object instances. This means that you can construct your own Exception
instances and provided custom arguments to them. You can access Exception
arguments using the .args
attribute. You have also looked at a long list of Python built-in Exception
classes, which you may choose to raise when needed.