r/learnpython • u/MonsieurBouclier • Mar 12 '24
Is python OOP the real thing ?
Hello, I'm currently writing a report on a project where I used python with the OOP paradigm and I want to be sure to not talk nonsense in it.
So, here is my question : Is python really fully respecting the OOP paradigm ?
I mean, for exemple when I want to make an abstract class, the concept doesn't really seem to exist. When I write an abstract method, I am obligated to use a decorator and it doesn't even prevent from calling the parent method. So i need to have a default behavior in it...
Also another question : I have seen that only mutable parameters can be updated in the body of a function. Is it specific to python or something generalized to other languages ?
4
Upvotes
9
u/Bobbias Mar 12 '24
OOP
The reply by u/cyberjellyfish is the best one as of writing this comment.
OOP is a nebulous concept, and modern definitions have strayed a fair bit from the intention of the creator.
However, ignoring that, Python still allows for modern OOP style programming.
It lacks some of the attributes some languages consider to be a core part of OOP, but that does not mean it automatically isn't OOP.
Abstract classes are nothing more than a class you cannot instantiate. From that perspective all your class needs to do is throw an exception when you try to instantiate it and it is now an abstract class.
The standard python equivalent to abstract classes (or interfaces, as they are sometimes referred to) are Protocols. Protocols cannot be instantiated by default (you could technically add
__init__(), but that defeats the purpose of making it a Protocol in the first place).If you have no default implementation for your abstract method, you raise a
NotImplementedexception in the Protocol class definition, and leave it up to the subclasses to implement.Python might not enforce the same rules that a language like Java, C++, or C# might, with regards to OOP, but that doesn't mean you can't write OOP code in Python.
Much of the difference with those languages comes from the fact that Python is dynamically typed. Consider the following code:
from typing import Protocol
This code is perfectly valid in python, because as the documentation I linked describes, python supports structural subtyping, meaning as long as
Bhas whatever attributesFooBardescribes, this code will function as expected, even ifBwas never explicitly named as a subtype ofFooBar.By comparison, this would not work in any of the previously mentioned static languages because none of them support structural subtyping.
Bwould be required to explicitly derive fromFooBar. That doesn't change the fact that we are passing one of two subtypes to a function which expects objects of the parent class, which is a pattern commonly used in OOP.This demonstrates that while Python may be different from some of the languages people think of when they think of OOP these days, you can still program the same kind of code that you typically use in those languages. In fact, you are less restricted in some ways due to the structural subtyping.
Mutable parameters
Most functions don't change their arguments. Methods often change the instance they are attached to, but freestanding functions should generally speaking not change their arguments with minor exceptions. Even then, you don't strictly need to mutate the list. You could just as easily build a new one with the required changes instead and return that. If your function mutates its input, it becomes more difficult to reason about:
I would argue that
foomakes a lot more sense thanbar. It's more clear about what's happening (we're building a new list starting with the contents of the old one and inserting the value5at the end), and it creates a new object. We could replacethe_listwith if we wanted, or we could save it to a different variable, or pass it directly to another function as I do here.With
barwe have no such control, it mutatesthe_listand there is nothing we can do to avoid that. If we wanted to keep the original list around, we would need to copy it to another variable before callingbar. It's return type also tells us nothing useful. Since there's no documentation we'd need to go read the definition to see what it's even doing. Ifbarwere a complex function which happens to work this way, it could take a fair bit of time to work out what's going on.Of course, the other kind of mutable parameter is mutable default arguments, but those are a foot gun that you should generally avoid unless you absolutely need to use one. They are instantiated exactly once, at the beginning of the script, and their state persists throughout the entire life of the script. Any changes you make to a mutable default parameter persist between function calls (unlike local variables).
In the case of immutable values like Tuples, consider the following code:
At first glance it looks like this should work the same as changing the list in the function
barabove, but because tuples are immutable, there is no way to mutate the value of a tuple once it's created. Any change tothe_tuplemust come from creating a new object and binding the namethe_tupleto that new object, rather than modifying the existing object it points to at the start of the code.