Skip to main content

@main_function

https://img.shields.io/pypi/v/main_function.svg https://img.shields.io/travis/vovavili/main_function.svg 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.

Example:

This (following from main_function import main_function):

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

Is fully equivalent to this:

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

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

https://discuss.python.org/t/built-in-is-main-function-as-a-more-beginner-friendly-alternative-to-if-name-main/18570

Credits

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

License

MIT

History

1.0.0 (2022-08-31)

  • First release on PyPI.

Metadata

Release files for main-function 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for main-function 1.0.6
File Size Uploaded
main_function-1.0.6.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for main-function 1.0.6
File Interpreter ABI Platform
main_function-1.0.6-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 14.8 kB

Release files / main_function-1.0.6.tar.gz

Download URL main_function-1.0.6.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
6d551df02ed2ff2aad465dfa433d70fcd538f56dd777f6978f668865de150806
BLAKE2b-256 checksum
How to use checksums
d8e4ef4bb3907b0e55d0f2a62ef85d824914166f0546cc34ad73a6d0a13ecf3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / main_function-1.0.6-py2.py3-none-any.whl

Download URL main_function-1.0.6-py2.py3-none-any.whl
Size 3.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
194fae2e1213a3c61760dd3375866f3106b42f75bc70b5faeddbb857e0b6a658
BLAKE2b-256 checksum
How to use checksums
3ed1ebddadde1e7c9375ddc42e8ff585dc36a177216c565cb52f98ecdbd15868
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release history Release notifications | RSS feed

This release

1.0.6 This release

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page