This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 11: Summary

Summary

face Josiah Wang

Ok, I think that is enough content for this lesson!

As usual, let’s recap what you have learnt in this lesson.

The main topic of this lesson is on creating custom classes and applying them to improve code readability. Try answering the following questions.

  • What is a class?
  • What is a class instance?
  • What are instance attributes or instance variables?
  • Why are the advantages of using a class definition over a dict to represent an object instance?
  • What is an instance method?
  • How do you use a class constructor to initialise attributes of a class?
  • What should the first input parameter of a method be?
  • What does the self variable refer to?
  • Name a few example Python magic methods or dunder methods.
  • Do you understand the difference between instance methods and functions? What advantage does instance methods have over functions?

We also covered various other topics. Can you answer the following questions?

  • What is a set and how is it different from list, tuple and dict?
  • Can you access an element of a set via indexing (i.e. my_set[2])?
  • Name some operators for sets.
  • Name some methods for sets.
  • What is a frozenset and how is it different from set?
  • What is a Python module?
  • How are modules useful?
  • How do you create your own module?
  • What are the different ways you can import from a module?
  • How do you ensure that your test code in your module does NOT run when you are importing the module from another script?
  • What is the __name__ variable and how do you use it?
  • Do you know how Python searches for a module when you import it?
  • How do you add a directory to Python’s module search path?
  • What are files?
  • What are the differences between text files and binary files?
  • How do you open text files for reading?
  • How do you open text files for writing?
  • How do you open binary files for reading and/or writing?
  • What are the different ways you can read from a text file?
  • How do you write to a text file?
  • How do you close a file?
  • What is the with statement for? And how does it relate to closing a file?
  • What is the main difference behind the LBYL and EAFP approaches to handling invalid input? Which one does Python encourage?

You have also refactored your robot project (yet again). This time you created a custom Robot class from your robot instead of using a dict. You have also converted some of your functions into methods for the Robot, hopefully making your code more intuitive to read in the process.

From the Git side, you have learnt how to ignore files that you do not want to be added to your repo. You have also created an online remote repository for your robot project from your local copy, and have learnt how to keep them in sync with git pull and git push.

That’s quite a lot covered yet again. Hopefully you are not yet bored and want to learn even more!

In the next lesson, we will continue where we left off with object-oriented programming, and start making your projects really object-oriented. We will also explore lots of advanced Python features that may make your life easier!

Take a break, and see you in the next lesson!