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

    Many copies are broken in discussions of why the python is kind of byaka - it 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 seemed that they were not enough, I think I finally understood how to explain this more clearly, I hope that this is true.

    Let me remind you that for private methods, python only dynamically changes the name and does not restrict access to it in any way, but for protected it does not do this, it’s just a naming convention for methods, for those who don’t really know, there are additional materials here and here .

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

    Привычное != логичное.

    What are the possible options?

    1. You are the author of the code, for some reason you decided that this method is not needed in the public domain, ok, your right to think so, but why should it be technically forbidden? Does this code call daemons and need to be hidden? Or is it regular code and another programmer like you might be in a situation that you can’t even think of and in which he needs to use this code. Or do you think that you are a superman and you absolutely know that this cannot be?
    2. You are a user of someone else’s code - usually you don’t know 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 knowingly and purposefully learned about the existence of some non-public method and called it, what happened? Did the earth open and Satan and Saddam jump out? Or did it work as expected? I hear someone screaming - it may break during the upgrade, of course, but since you deliberately used the non-public method, then you understand this risk (if not, then you are out of place). Moreover, updating is not a spontaneous process, like the decay of the nucleus of a uranium atom, everything in the test environment will break. Of course, some people

    In fact, behind all these arguments there is an important, one can say philosophical, principle: "The machine is dumb, the person is smart." Those. it is not the machine that must make decisions about what is possible and what is not, a decision must be made by a person.

    You can counter this by saying that the author of the code (a person or someone rational) decided that it’s impossible, but then another principle comes into play, the principle of freedom and responsibility: “If you have already shared something with others, don’t dictate how they are we must use it, they themselves are responsible for their actions ”, here 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 a machine tells a person, oh I'm stupid, I don’t know what to do - who and what type to lead to, kindly explain.

    By the way, there is a great example of a case when the authors decided that they can make the car smart, but they didn’t succeed - this is the YAML format, the authors wrote a large specification that few people seem to be able to implement or it’s not clear enough , but there are fundamental problems that are not related to the implementation when attempts to guess the type of soap (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 for the data there is a separate, human-written scheme that allows the machine not to pretend to be smart, but to know where what type, but unfortunately this approach has not become mainstream yet.

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

    Also popular now: