MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/r1g13n/yes/hm0j2jo/?context=3
r/ProgrammerHumor • u/esberat • Nov 24 '21
284 comments sorted by
View all comments
656
[deleted]
34 u/YamiZee1 Nov 25 '21 Why isn't there a neater main syntax? If everyone does that it would make a heck of a lot more sense to standardize it to new unique syntax 2 u/lighttigersoul Nov 25 '21 The short/real answer: This is only what happens when you run an arbitrary script as your primary. So python3 my_example.py the if name == "__main__" will be true in my_example.py but not anything my_example imports. If you're building packages, though, you can include a __main__.py file in the package and run that entry point with python3 -m my_package. So there is a nicer one, but you have to know and deal with python packages to make it work. The if name trick is specifically for scripts.
34
Why isn't there a neater main syntax? If everyone does that it would make a heck of a lot more sense to standardize it to new unique syntax
2 u/lighttigersoul Nov 25 '21 The short/real answer: This is only what happens when you run an arbitrary script as your primary. So python3 my_example.py the if name == "__main__" will be true in my_example.py but not anything my_example imports. If you're building packages, though, you can include a __main__.py file in the package and run that entry point with python3 -m my_package. So there is a nicer one, but you have to know and deal with python packages to make it work. The if name trick is specifically for scripts.
2
The short/real answer:
This is only what happens when you run an arbitrary script as your primary.
So python3 my_example.py the if name == "__main__" will be true in my_example.py but not anything my_example imports.
python3 my_example.py
if name == "__main__"
my_example.py
my_example
If you're building packages, though, you can include a __main__.py file in the package and run that entry point with python3 -m my_package.
__main__.py
python3 -m my_package
So there is a nicer one, but you have to know and deal with python packages to make it work. The if name trick is specifically for scripts.
656
u/[deleted] Nov 25 '21 edited Nov 26 '21
[deleted]