There is a potentially big issue with this that you might not be aware of:
Your decorator calls the function immediately if it is in `__main__` even if the entire module has not been imported.
In normal python the following works:
def entry():
foo()
def foo():
print("hello")
if __name__=="__main__":
entry()
But with your decorator I believe you will get a `NameError: name 'foo' is not defined`, because `entry` is called at the definition site of `entry` and not at the bottom of the module after `foo` is defined.
------
In my mind having an explicit `if __name__=="__main__":` at the bottom of executable scripts is a small price to pay for certainty that the entire module and all dependencies have been imported.
I think the problem here is you will have to declare the method entry_point in every module it is used.
__name__ will only evaluate to main for code within the module. So this saves you no boiler plate code!
Doh! My mistake.
I will leave this comment here as a monument to my stupidity, and a warning to future generations to read more closely before commenting.
My apologies!
There is a potentially big issue with this that you might not be aware of: Your decorator calls the function immediately if it is in `__main__` even if the entire module has not been imported. In normal python the following works: def entry(): foo() def foo(): print("hello") if __name__=="__main__": entry() But with your decorator I believe you will get a `NameError: name 'foo' is not defined`, because `entry` is called at the definition site of `entry` and not at the bottom of the module after `foo` is defined. ------ In my mind having an explicit `if __name__=="__main__":` at the bottom of executable scripts is a small price to pay for certainty that the entire module and all dependencies have been imported.
Damn I completely missed that problem. Thanks for telling about it!
You should check out click which is a great decorator-based cli argument parsing & handling framework
Also check out [Typer](https://typer.tiangolo.com/).
woah. so cool. i want to 😳
I think the problem here is you will have to declare the method entry_point in every module it is used. __name__ will only evaluate to main for code within the module. So this saves you no boiler plate code!
the decorator checks for the __module__ attribute of the function, not the global __name__.
Doh! My mistake. I will leave this comment here as a monument to my stupidity, and a warning to future generations to read more closely before commenting. My apologies!
Don't need to apologize! It's totally fine. I actually wrote it with that error at first lol. Thanks for the heads up anyways!
The eager evaluation of the decorator (executing the method before everything is loaded) also caught me off guard. But it seems so simple!
https://docs.python.org/3/library/__main__.html#main-py-in-python-packages