In this article I will review the main concepts a beginner Python programmer should learn about OOP in Python. The content is explored in detail in other posts on the blog that are linked at the end of each section.

Fundamentals

Object-oriented programming (OOP) is a paradigm, that is a way to structure your code that aims to make it easy to maintain and modify. Often OOP is considered opposed to functional programming (FP), but the truth is that both paradigms are useful and some modern languages implement both (Scala, for example). Python implements some features of the FP paradigm, but if you want to be a good Python programmer you have to understand OOP.

Moreover, since OOP is a conceptual paradigm, you have to learn how Python implements OOP, that is, the specific rules of the Python universe, which are not true in other programming languages.

The core concept you have to learn is that a paradigm like OOP has a single goal: that of making code reusable and easy to change. Keep it in mind, otherwise you will blindly learn rules without understanding, which means that you will not understand when you have to be strict and when you should bend them.

Three pillars

OOP has three main pillars:

  • Data encapsulation has been introduced to link data structures and functions working on them. Objects are created from classes, which promote the separation between private code (code used to manage the low-level details of the data structures) and public code (code available to other parts of the system).
  • Delegation is the way OOP promotes code reuse. Through delegation, objects in a system can collaborate and avoid duplicating code. It can be implemented through inheritance (implicit delegation) or composition (explicit delegation).
  • Polymorphism is how OOP promotes algorithm reuse. When polymorphism is correctly implemented, objects with completely different internal structures can be processed by the same algorithm and collaborate. This promotes refactoring and code maintenance allowing to replace old code with new implementations.

Resources

To learn how Python implements these three main concepts you can read the following posts:

These are all part of the same series of posts, so you can read them in order to have a complete picture. The same concepts are also explained in the following videos that I published on YouTube:

To get more comfortable with classes and delegation you can review them with these two simple articles:

The following posts are simple but very useful exercises of OOP design, and they also use test-driven development (TDD)

Speaking of TDD, you should get used to mocks, that are extremely useful when it comes to test objects that work together:

Before you move on to more complex concepts you might want to have a look at what you can build with these basic concepts of OOP and TDD:

If you made it so far you are ready to face the big players! The following are concepts for advanced programmers, so be sure you are comfortable with the previous sections before venturing here. Don't be too scared, though. These concepts are advanced but not impossible, and if you want to get better at Python you should at least understand what they are.

Final words

Wow, this was a nice trip! Obviously, I don't think I covered all that can be said on OOP in Python, but I'm pretty sure that all these posts can boost your knowledge of this important paradigm and allow you to become a better Python programmer. I will update this post when I will publish new articles on OOP here on the blog.

If you found this useful, please consider sharing it with a friend!

Feedback

Feel free to reach me on Twitter if you have questions. The GitHub issues page is the best place to submit corrections.

Image by LoggaWiggler from Pixabay