Skip to main content

A beginner-friendly decorator alternative to if __name__ == '__main__': main() idiom.

Project description

@main_function Documentation Status Updates

A beginner-friendly decorator alternative to if __name__ == '__main__': main() idiom.

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.


This (following from main_function import main_function):

def main() -> None:
    print("Hello, world!")

Is fully equivalent to this:

def main() -> None:
    print("Hello, world!")

if __name__ == "__main__":
For further discussion that has inspired this PyPi, please see this:


This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.




1.0.0 (2022-08-31)

  • First release on PyPI.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

main_function-1.0.6.tar.gz (11.0 kB view hashes)

Uploaded source

Built Distribution

main_function-1.0.6-py2.py3-none-any.whl (3.8 kB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page