OOP in Python

When people first learn Python, OOP (object-oriented programming) is one of those things they start to use before fully understanding it. That makes sense, because OOP is a huge topic. You really have to use it for a while before you can fully understand it in any real depth.

This series aims to pull back the curtains on OOP in Python. We’ll take a closer look at the things we see in object-oriented code, and we’ll see how things work internally. We’ll look at some aspects of OOP that you might not have started to use yet. In the end, you should be able to make better sense of the code you’re already working with, and write cleaner code when you’re working on fresh projects.


This series has 20 parts:

MP #36: What is object-oriented programming? Why is it important in general, and why is it important in Python?

MP #37: What is self? Why is it important? What would a world without self look like?

MP #39: What’s so special about the __init__() method? Does every class need an __init__() method? Why is it so important to spell __init__ correctly?

MP #41: What are static methods? When should you consider using them, and why would you want to?

MP #42: What are class methods? What should you use them for? What should you be careful about when using them?

MP #44: What are the __str__() and __repr__() methods used for? When should you use them in a class?

MP #46: What does the __new__() method do? When would you want to write your own __new__() method?

MP #47: How are Python’s comparison methods implemented? How can you override them, to assign custom actions to symbols like >, <, and other operators?

MP #49: What are helper methods? How can you use helper methods to keep longer classes readable and maintainable?

MP #50: How do you organize your classes? There are no hard rules, but there are some common patterns to be aware of.

MP #52: Why is inheritance so important in OOP? How does super() work?

MP #53: How do you enforce constraints on subclasses? This post introduces abstract base classes, which can set rules that subclasses most follow.

MP #55: How do you convert instances of a class to a form that can be written to a file, or transferred over the internet? How do abstract base classes enforce structure in a real-world project?

MP #57: How are exceptions implemented in Python? How does the Exception class hierarchy enable the behavior we see with exception handling in Python?

MP #59: How is the pathlib module implemented? How are OOP principles used to manage the similarities and differences between the macOS/Linux and Windows filesystems?

MP #61: How are classes used in Matplotlib? How is the internal class structure of Matplotlib related to the tremendous flexibility that the library offers?

MP #63: What is composition? Why does composition lead to fewer issues over time than inheritance?

MP #64: How do you use composition and inheritance together? How does this mix lead to code that’s easier to understand and maintain?

MP #66: When do you need to use OOP? And when do you not need it?

MP #67: What were the most important takeaways from this series? What else would be included if this series went on a little longer?


If you have any questions about what you find in these posts, please feel free to leave a comment and I will be happy to respond.