DEV Community

Discussion on: How to Use Object-Oriented Programming in Python

Collapse
phantas0s profile image
Matthieu Cneude • Edited on

Interesting article! I'd like to discuss on some points, however.

When you say:

Many developers use OOP because it makes your code reusable and logical, and it makes inheritance easier to implement. It follows the DRY principle, which makes programs much more efficient.

This is debatable. First, because I don't think inheritance is something very valuable for a beginner; it's hard to use properly. OOP doesn't follow the DRY principle per se: if I want to create five different classes to "code" the same knowledge, I can if I want to.

The four main principles of OOP are inheritance, encapsulation, abstraction, and polymorphism.

Inheritance is definitely proper to OOP. However, polymorphism (inheritance is a flavor of it), abstraction, and encapsulation can be achieved with other paradigms. The real difference with OOP is this: local persistent variables (that's why you can have two properties with the same name in different objects and mutate one without affecting the other), inheritance indeed, and message passing (you can call two functions with the same name on different objects, you'll call two different functions; when you do obj.add(), you send the message add to an object and it might return something).

OOP is mainly used because, historically, C++ was heavily used. Why? Not because it was better than anything else, but mainly because it was derived from C and compatible with C. And C was heavily used. There are other reasons, too; the very good talk Why C++ Sails When the Vasa Sank gives very good insights on that.

From there, everybody was using OOP, so for a language to be successful, it had to have some implementation of OOP as well.

Last thing: a paradigm is a set of idea, and it's very difficult to define it. Smalltalk is totally different from C++ which is different from Golang which is different from Python, for example. But they are all considered OOP languages.