This will test to see if your python module (file that ends in .py) is being run from command line or being imported by another module. If you are running your file directly from the command line, the the variable __name__ is set to __main__ . If, however, your module is being imported by another modlue, the __name__ is set to be the module (file) name.
You can use this line at the end of your code to test classes, among other things. FOr example, here is cow.py:
class Cow:
def moo(self):
print(‘MOO!!!’)
def walk(self):
print(‘waddlming!!!’)
def main():
bessie=Cow()
bessie.moo()
bessie.walk()
if __name__ == ‘__main__’: main()
Here is the output:
python3 cow.py
MOO!!!
waddlming!!!
The following site explains all this well: