if __name__ == ‘__main__’: main()

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):

def walk(self):

def main():

if __name__ == ‘__main__’: main()

Here is the output:
python3 cow.py

The following site explains all this well:

View at Medium.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.