⬆️ ⬇️

Python does not prohibit calling private / protected methods because it loves you :-)

Many copies are broken in discussions of why Python is such a bjaka - does not prohibit calling non-public methods. And of course, the explanations in the spirit of “we are all adults here” sounded more than once, but it seems they were not enough, I think I finally understood how to explain it more clearly, I hope that this is true.



Let me remind you that for private methods, Python just dynamically changes the name and does not restrict access to it in any way, and for protected it doesn’t do that either, it’s just a naming convention for methods, for those who don’t know much, there is additional material here and here .



First, you need to start with a question, but why should a language prohibit calling some kind of code? Many languages ​​do this, but it does not follow from this that it is right and should be so.



!= .



What options are possible here?

')

  1. You are the author of the code, for some reason decided that this method is not needed in public access, ok, your right to think so, but why should it be technically prohibited? Does this code call demons and hide it? Or is it a normal code and another programmer like you may find yourself in a situation you cannot think about and in which he needs to use this code. Or do you think that you are superman and absolutely know for sure that this can not be?
  2. You are a user of someone else's code - usually you will not learn anything about the existence of non-public methods unless you make special efforts (protected methods can be documented, but with an indication of what they are). Well, let's say you consciously and deliberately learned about the existence of some non-public method and called it, what happened? The earth opened and Satan and Saddam jumped out? Or did it work as expected? I hear someone screaming - it can break when updating, of course, but since you purposefully used the non-public method, then you understand this risk (if not, then you are not in your place). Moreover, updates are not a spontaneous process like the disintegration of the nucleus of an atom of uranium, everything will break down in the test environment. Of course, some people have that updates are of a quantum nature and occur at the most unpredictable moments in the most unpredictable places, but then it’s ridiculous to talk about private methods of any kind, problems will be complete without them.


In fact, behind all these arguments there is an important, one might say philosophical, principle “The machine is stupid, an intelligent person.” Those. it is not the machine that must make decisions about what is possible and what is not, the decision to be made to a person



You can parry this by saying that the author of the code (a person or someone sensible) decided that it is impossible, but another principle comes into play, the principle of freedom and responsibility: “If you have already shared something with others, do not dictate to them how should use it, they are responsible for their actions ”, then we just came to the conclusion that we are all adults here and are responsible for our actions.



Someone will say that typing is the same restriction - true, but this is just the case when the machine says to a person, oh I'm stupid, I don’t know what to do - who and what type to bring, please explain.



By the way, there is an excellent example of the case when the authors decided that they can make the car clever, but they did not work out - this is the YAML format, the authors wrote a large specification that few people seem to manage to implement or it is not sufficiently straightforward , but there are fundamental problems not related to the implementation when the attempts of the pit to guess the type (to pretend that the machine is smart) lead to a fiasco.



In the python world there was a man who understood this (and described in detail). His library just looks like the right approach - the data is human-readable, but there is a separate human-written scheme for the data that allows the machine not to pretend to be smart, but to know where what type is, but unfortunately so far this approach has not become mainstream.



I hope I managed to improve the explanation of the python philosophy and make fun of other approaches)

Source: https://habr.com/ru/post/457034/



All Articles