A few weeks ago, I completed a technical test in Python. While the overall feedback from the test was very positive, one of the things I was surprised to read in the feedback was:
No encapsulation – plenty of public_functions which should be _private_functions
This surprised me because - according to the vague picture I'd built up in the back of my mind over my two years as a Python developer - we don't really do private methods or functions in Python. All that we do have is a patchwork of conventions, shorthand and gentleman's agreements. But is that picture in the back of my mind correct?
So I've looked into the matter a little more deeply, consulting the standard Python documentation as well as PEP 8, and I've come to the following conclusions:
__spam
will be interpreted as _classname__spam
. This can be used to emulate a crude kind of privacy, such that if one tries to call __spam
from outside of its class, an exception will be raised._single_leading_underscore
will not be imported when from M import *
is called, but I couldn't find anything else.However, this only tells me what I could do, not what I should do.
Can I write off the above feedback as coming from someone who's probably spent his professional life programming in Java/C#/etc, and therefore doesn't have the correct mindset when it comes to encapsulation in Python? Or should I re-think the way I write Python programs, so that any encapsulation is much more explicit?
From technical point of view, there is no right or wrong way of writing code. What computer cares about are instructions and data. If you use clases or not, if your code is modular or spaghetti, what are the names of your functions, if they are public or private etc. - it all goes down the drain by the time it reaches the CPU.
Arguments like these are arguments about opinions. Everybody has an opinion. You might also call it taste. People with similar opinions group together and create organizations, political parties, institutions etc. to promote their ideas. They claim that it is somehow possible to generalize and distill how software should be written, into decisions that are either "right" or "wrong".
Yet, for every rule that proves something, you will quickly find another rule that contradicts it. This is just the law of nature - reality knows no boundaries. This is the reason of all confusion.
There is no such thing as universal rule about something, and there never will be. It would contradict the very nature of the universe: time and entropy. Whatever question you ask, the answer is always "it depends".
After 40 years of programming my core values are down to this: