redradist@gmail.com wrote:
Hi all, In Python we often use the following syntax to call the main logic of script when it was ran: def main(): pass # whatever should be done for `python ./script.py`
if __name__ == '__main__': main()
Maybe it is a time to introduce the new module level function like __main__ ? Consider the following code: def __main__(): pass # whatever should be done for `python ./script.py`
It is much easy an less code to write ;) Under-hood Python just will generate the following code: def __main__(): pass # whatever should be done for `python ./script.py`
# Below generated code if __name__ == '__main__': __main__()
If there are two if __name__ == '__main__': it is also not an issue: def __main__(): pass # whatever should be done for `python ./script.py`
def main(): pass # whatever should be done for `python ./script.py`
if __name__ == '__main__': main()
# Below generated code if __name__ == '__main__': __main__()
Or we could require user to have only one if __name__ == '__main__': ... What do you think, please share your opinion ...
While is possible to use if __name__ == '__main__': several times in the same script, your proposed magic function def __main__() cannot be redefined. Not to speak about lexical scope differences between one approach and the other. So your proposal is reducing features instead of expanding them. (Sorry for repeating the message. The first one went to another thread)