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.
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.
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.
To learn how Python implements these three main concepts you can read the following posts:
- Data encapsulation: Object-Oriented Programming in Python 3 - Objects and types and Object-Oriented Programming in Python 3 - Classes and members
- Delegation: Object-Oriented Programming in Python 3 - Composition and inheritance
- Polymorphism: Object-Oriented Programming in Python 3 - Polymorphism
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:
- Video Object-oriented programming in Python - Part 1 - Basic concepts
- Video Object-oriented programming in Python - Part 2 - Types and classes in Python
- Video Object-oriented programming in Python - Part 3 - How to create a class
- Video Object-oriented programming in Python - Part 4 - Classes and instances
- Video Object-oriented programming in Python - Part 5 - Delegation: inheritance and composition
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)
- A simple example of Python OOP development (with TDD) - Part 1 and A simple example of Python OOP development (with TDD) - Part 2
- Refactoring with tests in Python: a practical example
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:
- Clean architectures in Python: a step-by-step example which evolved in my book "Clean Architectures in Python" available online or as PDF/ebook
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.
- Metaclasses: Object-Oriented Programming in Python 3 - Metaclasses and Advanced use of Python decorators and metaclasses
- Abstract Base Classes: Object-Oriented Programming in Python 3 - Abstract Base Classes and Python decorators: metaprogramming with style
- Multiple inheritance: Multiple inheritance and mixin classes in Python
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!
Accessing attributes in Python
Method overriding in Python