If you write a program in OOP style in Python, is it worth going deep into OPP, avoiding constants (not singltons) and usual functions (not methods) or should it be combined?

A real-life task: I need to build a small but expandable service on the Flask framework. What am I doing:

  1. I make a file with routes (distribution) of urls.

  2. I make the handlers module in which I add all the handlers of routes. They are presented in the form of classes, since This allows you to use inheritance and other OOP tools.

  3. Understandably, the models module, which contains a database view.

  4. A small module in which I connect routes to the handlers of these routes. Also here I can specify some general settings for the handler, for example, logging.

So, isn't it better than writing a bunch of functions that both accept the request and immediately process this request? And if there is a more optimal and flexible way to do this, could you tell?

  • one
    in the in. when you get used to writing in other languages ​​in oop style (by interfaces and dependencies, that is, contracts), then in a python you get a stupor where the code pulls dependencies on itself. - vitidev
  • @vitidev I already discussed with someone that it is not necessary to write Java in the Python. This idea is not new, people a decade ago still tried to Fortran in the then new today already forgotten languages ​​to write. Some things are universal, some things are specific to the language — you need to change habits when you switch the language. It is better not to think about how to call a programming style, but think how much we read the code for people familiar with Python (or for you in six months when the details are forgotten), how easy it is to fix bugs, add new functionality, without breaking it, how much it is productive for your task. - jfs
  • @jfs you discussed with me. And I asked you absolutely not java-specific things. The conversation was about design principles "when everything is on contracts and implementation details are hidden, thereby ensuring low coherence (OOP + DI) and how to be in python, in which the module is the basic unit and the module is directly addressed there was no ideology, but not to rewrite into classes at one time. " The author asks the same question, only not as profoundly as I (I gave a concrete example). But, alas, you didn’t understand my questions and your answers were in fact useless. - vitidev
  • one
    @vitidev: my comments are not so much for you, but for other people, so that you do not mislead them. I quoted a direct quote from your comment. If you don’t want to learn how to use the Python package — your business. This is not some kind of exotic material - the modules in the introductory guide are described - jfs
  • one
    @faoxis That's not the point. Simply modular python (dependency pulling) leads to a "config-oriented" design, which is not a problem for scripting, but for most things you will have to take this feature into account and either resort to magic or rewrite the variable part. Config-orientedness is unusual for the OOP supporter, because he immediately thinks “what if this part has to be replaced, but let me have dependency inversion here.” If you do not think about it until the need comes, you can write. - vitidev

1 answer 1

Avoid the usual functions just do not. There is an opinion that in python it makes no sense, for example, to use static methods, since the style will be better if you use functions instead.

But in python, no one imposes anything. There are modules for constants and for enumerated types in the built-in library. For example, I always use enum where necessary, just for my own comfort. What is this about? It is more convenient to add functionality for a class than for a list of constants.

That is, I want to write in a clean, clean OOP, but for God's sake, there is everything you need for this.

  • it remains to find out what is a "clean-clean oop." - KoVadim
  • @KoVadim is when you write classes, even if they are used in one instance. - vitidev
  • one
    Alan Kay is indignant about your explanation. Let's just say a simple class for every sneeze - it's hot, not un. - KoVadim 3:49
  • @KoVadim Zhava is a language, and oop is the principle of code design. I didn’t say anything about “every sneeze”, but I said that what can be written by a module (it will still be a single instance), you still write as a class. Because a) contract b) flexibility. Contract .... I can collect the necessary graph of objects and replace any of them, because the dependencies are pushed through into the constructor and comply with the contract. Flexibility .... WebClient class where you can set the headers instead of requests, get where you need to constantly have these headers as a parameter to drag. - vitidev
  • You did not understand. It is possible to have classes without classes and classes without classes. - KoVadim