Advanced Lesson 2
Advanced Object-Oriented Programming
Chapter 5: Advanced Python type features
Conclusion
And we’re done with this short lesson!
Quick recap - we first introduced the acronym APIE, representing the four pillars of OOP: Abstraction, Polymorphism, Inheritance, Encapsulation.
Since we have already covered abstraction and inheritance, I focussed on discussing the remaining two concepts.
I first discussed polymorphism, which means “many forms”. I talked about how polymorphism can be seen when you override the method of a superclass, and also how you can use a method/attribute without knowing the specific object type (e.g. any weapon can be used to hurt an animal).
We then looked at the idea of abstract classes and abstract methods, where a more ‘abstract’ class leaves the implementation of a method to its more ‘concrete’ subclass. We also talked about duck typing, where you do not even care about the type of an object, as long as they implement the methods that you need. These are all forms of polymorphism.
We then turned to the last pillar - encapsulation. We discussed why we sometimes need to encapsulate our attributes so that they are not publicly accessible. We looked at the concept of getter and setter methods, and then converted these to the more Pythonic properties.
We then discussed class attributes, class methods and static methods. Hopefully you know the difference between these and instance attributes/methods!
Finally, we looked at some more advanced Python features to make your OOP implementation easier. We looked at type hinting, named tuples and data classes.
And that’s about it! There’s nothing much important to cover. You will probably pick up even more tips while you gain more experience - but hopefully you already have enough basics to be able to navigate the world of OOP. The main point from here is to just practise! You will start to master OOP once you start designing your own classes and figuring out the best way to refactor them. That itself is a whole new course!