5 ms·
> __name__ == "__main__" What makes this weirder than other languages detecting if they’re an import or an invoked file?
by zmgsabst 22d ago
> __name__ == "__main__"
What makes this weirder than other languages detecting if they’re an import or an invoked file?
- scoofy 22d agoIt’s only weird syntactically. Python tends to lean towards intuitive, natural language by default. I would have assumed they’d have created a syntactically straightforward alternative, like: __this_file__ == “__launch_file__” or similar. I understand python values “only one way” of doing things, but it would be helpful for readability.
- wodenokoto 22d agoNo, the “readable” shortcut would have been if __main__: There are no magic strings to remember and the IDE can help you look up variables.
- scoofy 22d agoAgain, this is a CS/Math major kind of thinking. The term __main__ means nothing syntactically. If we care about language acquisition -- thus language adoption -- it is always better to make "very common terms" in the language understandable to someone who is completely illiterate to your formal language. I honestly think one of the main reasons why python is so successful is as simple as: print "Hello world!" ...and later: print("Hello world!") It might not seem like much, but when you're asking someone to pick up a language outside of a classroom setting, it's just much more intuitive than: echo "Hello world!"; or: console.log("Hello world!"); or god forbid: public class GFG{ public static void main(String[] args) { System.out.println("Hello world!"); } } These little things matter in the long run, even if they don't seem like they matter when you're already fluent.
- frollogaston 21d agoIdk what's better, console.log or print, cause way back as a beginner I was confused what it means to print something. Like with ink? They're both ok, unlike the C++ or Java ways. But Python breaking hello world in version 3 was crazy.
- scoofy 21d agoAgain, I would argue “very common commands” like printing text should be so unbelievably intuitive that beginners can use them even before learning what a “console” is, why there are “logs” and how dot notation works.