Use this decorator to remove the UGLY "if __name__ == '__main__':"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | '''
entrypoint.py
2011 JUL 14
@author: Yoav Glazner
'''
def entry_point(f):
'''
example:
@entry_point
def main():
print("hello world")
'''
if f.__module__ == '__main__':
f()
return f
|
It seems more like more trouble then putting if __name__ == "__main__" to me.
Will this work properly when the
main
function is at the top of file instead of at the bottom after all the classes and functions have been defined?No, it needs to be at the bottom, just like normal.
Clever bit of code.
Pass command line args to function, return exitcode to OS: