r/learnpython • • 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

35 comments sorted by

View all comments

6

u/Solonotix Mar 12 '24

So, here is my question : Is python really fully respecting the OOP paradigm ?

Yes. The history of Object-Oriented Programming (OOP) as a paradigm is generally considered to have its beginnings with the Smalltalk language in the 70s. A Smalltalk object can do exactly three things:

  • Hold state (references to other objects).

  • Receive a message from itself or another object.

  • In the course of processing a message, send messages to itself or another object.

The paradigm is focused on the practice of encapsulating data (or messages) within an "object", and then expressing actions (or methods) by way of passing messages between objects. The nature of OOP is to return nothing from a function/method, with the exception of status codes to represent conditional states upon completing an action. Even then, the state is contained within the objects, and you were supposed to pass objects to other objects, never working with primitives outside the object's implementation.

Python is a multi-paradigm language, in that it has functions as first class (allowing for strictly Functional Programming), but it also has a Data-Object Model for working in Object-Oriented Programming. Where it struggles is encapsulation. There are a few ways Python tries to hide things from the user, but ultimately there are no access modifiers (that I know of). As such, all members are technically available if you're clever (I say clever because prepending a member with double-underscore will mangle the name in an attempt to prevent outside access). Other languages with access modifiers have workarounds such as reflection, but Python will let you access a member if you know its name without any manipulation by the caller