M
M
mRelby2020-05-25 00:00:14
Python
mRelby, 2020-05-25 00:00:14

Classes and OOP: why, and most importantly, when to use and when not?

Good day to all. I hope at least here I will be given a clear answer to my question.

Having read and watched more than a dozen different articles and videos on YouTube about Python in particular about OOP, I have never heard an answer to a question that seems to me quite important:
why , and most importantly, when should classes be used , and when is enough will write some simple functions.

Suppose . I want to write a tg bot that sends the user a video downloaded from ... well, for example, from KinoPoisk .
That is, the user sends a link to the movie to the bot, and the bot does everything necessary and eventually sends him a vidos (trailer) for this movie.

In my opinion, the task is simple. You need to write a few functions and the bot is ready.
I understand that everyone is now turned on this OOP, and most programmers will say that OOP is cool blah blah blah. It's all clear. But, the question is: why should I create classes for such a task?

I would be grateful if someone could explain this to me in simple terms.

ps if you set a bad example - provide your own example, and explain what and how.

Thank you .

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Armenian Radio, 2020-05-25
@gbg

All these classes, factories, abstractions and other buzzwords are needed so that the programmer can control the complexity of the program he creates. Our consciousness stores an average of 7 different entities, so by cutting the task into finished pieces - objects, we give ourselves and colleagues the opportunity to reduce the number of entities that need to be kept in our heads.
Further, the program, cut into parts, allows the replacement of one part by another. This makes it possible to develop a program, gradually increasing complexity and replacing pieces that have not yet been written with plugs, testing it, replacing a complex implementation of something with a predictable plug, and moving ready-made pieces of code from place to place, without rewriting the entire program again in case of a slight change in logic.
When such an approach is justified - almost always. A program with a well-organized internal structure is much more flexible and better supported than a long sheet without such a structure.

E
Ernest Faizullin, 2020-05-25
@erniesto77

Firstly, the OOP principle is needed when you want to sort everything out like a perfectionist, and secondly, when the project has more than one or two entities and their instances
. For example, a question and answer service

questions (hasMany answers)
    answers (belongsTo question)
        likes (belongsTo answer)
        comments (belongsTo answer)

In order not to produce repetitive code, a model (template) is created with which you can create the required number of instances. All frameworks have logic for connections between these instances according to the hasMany/belongsTo principle.
There are also the concepts of interfaces, abstractions and a description of their work in service providers (this should be googled, see examples).
Thus, we structure the code and the data with which the code works. And this is all a must have if you want to show your skill.

A
Adamos, 2020-05-25
@Adamos

A simple analogy of the difference between procedural and OOP.
Here you are writing a scientific article in which there is a certain main text, but every now and then there are references to the details of experiments, lengthy quotations from other people's works and a bunch of tables of several pages each.
The procedural approach is like a book: the whole text is a continuous sheet, and in order to follow the course of the main idea, you have to scroll through the details.
OOP is hypertext: in front of you is only the main material, not littered with anything. If you want to clarify some details, you can easily follow the link and work with them. If they don't interest you, then they don't bother you.
So, after a certain amount and complexity, working with a "book" (for example, finding a specific table and checking its data) is hard even for the author.

S
Sergey Vodakov, 2020-05-25
@WaterSmith

why, and most importantly - when to use classes, and when it will be enough to write a few simple functions.

Why - it has already been answered here, for the sake of structuring the code and simplifying support, scalability and automated testing.
When - if you are sawing your project, then when you feel that you need it. When you realize that you can't keep procedural code in your head. Then you will completely rewrite your code in the OOP style and you will no longer have such questions. Naturally, if you have a small project that fits into several procedures, then you will not come to this, but it is not necessary there.
But if you work in a team, then the existing code base will show you everything. It is difficult to write a procedural part of a project built on OOP.

D
Dmitry, 2020-05-26
@TrueBers

https://en.wikipedia.org/wiki/Fundamental_theorem_...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question