Dec-28-2017, 02:05 AM
if __name__ == '__main__': obj.method()This allows the program to execute code differently, depending on how it's started
To illustrate how this works, create the following two very short program:
short.py
def func1(): print('__name__: {}'.format(__name__)) func1()stillshort.py
import short shortNow from a command line, first run:
python short.pyresult:
Output:__name__: __main__
now run:python stillshort.pyresult:
Output:__name__: short
So, the if __name__ == '__main__':will only be true if the program was called directly.