On Sat, Oct 02, 2021 at 08:33:59PM -0600, Finn Mason wrote:
I've found that the `if __name__ == "__main__"` idiom is unintuitive and feels unnecessary,
The Python community thinks nothing of writing code with metaclasses, functions and classes as first-class citizens, recursion, parallisation using threads and process, concurrency with asynchronous coroutines, implementing astonishingly clever algorithms that rely on deep mathematical properties that literally require a decade or more of study to understand, and all that is fine, but checking whether a global variable is set to a particular value is too unintuitive? Of course nobody is going to *intuit* that the global variable is called `__name__` or that the value is `"__main__"`. But neither is anybody going to intuit that the way we load a single function from a module is from module import function as opposed to, let's say, "laden functie van onderdeel". Or that stdout is in the sys module. Or that floating point maths violates associativity: (0.17 + 0.69) + 0.97 != 0.17 + (0.69 + 0.97) These are all things we have to learn, not intuit. The first time I saw Python code, after being told that it was "really intuitive", I couldn't make head or tails of what on earth it was about, especially all those curly braces and square brackets and colons. And what's self, or a class? To me, a class was what you were in when you went to school, or maybe a socio-economic group. If Python was so intuitive, maybe it was just me, I was an idiot for not being able to guess the meaning from first principles. So I think that we sometimes take "unintuitive" too seriously.
I never liked how you can't import your package in its own __main__.py file (or maybe I'm missing something, packaging isn't my strong suit)
I would expect that either a relative import: from . import function or an absolute import: from mypackage import function import mypackage should work. -- Steve