Lesson 10
I am Your Father
Chapter 10: Summary
Summary
And that is the end of Lesson 10. Let’s recap!
The main topic of this lesson was inheritance in object-oriented programming. Here are some questions for you to ponder:
- What do we mean by a generalisation relationship between two objects?
- What do we mean by inheritance?
- How does inheritance help with reusing existing code?
- How does inheritance help with extending the features of a class?
- What is a superclass?
- What is a subclass?
- How do you define a subclass in Python?
- How do you call a superclass’ constructor from a subclass’ constructor?
- Can a subclass have its own methods (not defined in the superclass)?
- What does method overriding mean? How is it different from the previous question?
- How is method overriding useful? (Think weapons and old ladies attacking an animal!)
- How do you invoke the superclass’ version of a method from a subclass?
You have also tried applying inheritance to your robot project by creating a LeapingRobot
that can leap to the end of a grid with a single bound! Hopefully you will nurture and grow your robots in the future at your own leisure!
We then turned to more advanced, Python-specific topics relating to functions. Here are some questions to ponder.
- How do you use the built-in
any()
andall()
functions? How do they relate to theand
andor
operators? - How can you make a Python function receive an arbitrary number of positional arguments?
- How can you have a Python function receive an arbitrary number of keyword arguments?
- How do you force a function to receive keyword-only arguments? What about position-only arguments?
- What are lambda functions?
- When will a lambda function be useful compared to a traditional function defined with the
def
keyword? - How do you use lambda functions in the
sorted()
,min()
andmax()
functions, as well as thelist.sort()
method? - What are higher-order functions?
- Name two example higher-order functions that come built-in with Python. Do you know how to use these functions?
- Do you know how to use lambda functions with these higher-order functions?
- Are you able to write the list comprehension equivalent of these higher-order functions?
We also discussed further file handling beyond text files and JSON.
- What is a pickle file?
- Do you know how to load a pickle file?
- How do you save a pickle file to your hard drive?
- What are the different ways you can read from a CSV file using the
csv
module? - What are the different ways you can write to a CSV file?
We also looked at using the unittest
module for easier automated testing. You can easily run multiple test cases, provide more informative assertion error messages, and set up text fixtures.
Finally, we ended the lesson by going back to our main topic (inheritance), as applied to exception handling. If the built-in Python Exception
s do not fulfil your need, you can create your custom errors by extending the Exception
class. The class is like any other class, so you can define more attributes/methods as you see fit. Beware of the order of catching the exceptions if your class is a more specific subclass!