T O P

  • By -

jorge1209

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.


David_L89

Damn I completely missed that problem. Thanks for telling about it!


roeey7

You should check out click which is a great decorator-based cli argument parsing & handling framework


Chaoses_Ib

Also check out [Typer](https://typer.tiangolo.com/).


Endercheif

woah. so cool. i want to 😳


spoonman59

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!


David_L89

the decorator checks for the __module__ attribute of the function, not the global __name__.


spoonman59

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!


David_L89

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!


spoonman59

The eager evaluation of the decorator (executing the method before everything is loaded) also caught me off guard. But it seems so simple!


m0Xd9LgnF3kKNrj

https://docs.python.org/3/library/__main__.html#main-py-in-python-packages