Main_function – A newbie-friendly alternative to if __name__=='__main__':main()
2 points by vovavili 2 years ago | 4 comments- pestatije 2 years agoThis is what I call a confuser, not a helper.
If I'm a newbie, I'll have to find out what that annotation is. At which point I'll have to understand the very point the annotation is trying to hide!
- gchamonlive 2 years agoI think that in the roadmap hierarchy of what you should learn as a beginner, annotations should come way earlier than python inner workings like __name__.
I for one think this is a great idea and should be added to the core language libraries.
- vovavili 2 years agoYes, that exactly was my idea - a decorator still is confusing, but I hope that it is less confusing and more visually appealing to a beginner that an idiom with magic methods.
- vovavili 2 years ago
- gchamonlive 2 years ago
- vovavili 2 years agoHello all! I suggested a certain enhancement to Python, and Python developers have directed me to make a package first to see how popular this proposal would be, so here I am.
GitHub - https://github.com/vovavili/main_function
PyPi - https://pypi.org/project/main-function/
This PyPi is meant to demonstrate a decorator-based alternative to the old-school idiom, if __name__ == '__main__': main(). Traditionally, this idiom indicates that a script is meant to both be run directly and be imported as a module, but its (possibly unjustified) prevalence in educational materials nudges beginners to wrap their head around magic methods and how Python works under the hood before it might be appropriate. Purely from cursory overview of beginner-to-intermediate educational content available for free, the idea of using magic methods for this specific purpose does seem to be the biggest source of confusion for people new to Python - as of the moment of writing this, YouTube video explaining what this bit of code does by Corey Schafer is sitting on 1,7 million views (one of his best), and video explaining the same concept by mCoding is at 865 thousand views (his most viewed video ever). Question asking what this idiom does on StackOverflow has more than 7600 upvotes, and more than 3200 bookmarks (second most upvoted Python question). Furthermore, this improvement fits in line with introduction of dataclasses, which is basically a way of declaring classes without writing boilerplate magic methods.
This very telling, self-explanatory decorator provides an alternative for people who don’t want to remove the if __name__ == "__main__": main() idiom altogether, be it for lack of understanding, concerns about future extensibility or any other reason.
Example:
This (following from main_function import main_function):
Is fully equivalent to this:@main_function def main() -> None: print("Hello, world!")
For further discussion that has inspired this PyPi, please see this:def main() -> None: print("Hello, world!") if __name__ == "__main__": main()
https://discuss.python.org/t/built-in-is-main-function-as-a-...
Please let me know your thoughts, or join the discussion with actual Python developers using the link above.