Learn Enough Python to be Useful Part 2

Learn Enough Python to be Useful Part 2How to Use if __name__ == “__main__ “Jeff HaleBlockedUnblockFollowFollowingFeb 7This article is one in a series to help you become comfortable in Python scripting land.

It’s for data scientists and anyone new to Python programming.

cat_breed = maine_coonif __name__ == "__main__": is one of those things you see in Python scripts that often isn’t explained.

You might have seen a hundred tutorials on Python list comprehensions, but Python scripting conventions get short shrift.

In this article I’ll help you connect the dots.

state = maineif __name__ == "__main__": ensures that the code in the if block runs only when the file in which it is found is run directly.

Let’s break down how this works.

What is it?Every Python file gets a special attribute called __name__ .

Test it.

Create a Python file my_script.

py with print(__name__) as the only content.

Then run python my_script.

py in your terminal.

__main__ is what you’ll see.

Note, those are double underlines on both sides of name.

Those are called dunders for double underscores.

They are also know as magic methods or special methods.

You might be familiar with dunders from the __init__ method used to initialize an object of a class you’ve created.

Why use __main__?If you import a file as a module the code doesn’t run.

The code only runs when that file was run AS ITSELF.

Check out this example.

Code available on GitHub.

# my_scriptprint(f"My __name__ is: {__name__}")def i_am_main(): print("I'm main!")def i_am_imported(): print("I'm imported!")if __name__ == "__main__": i_am_main()else: i_am_imported()Running the program in the terminal with: python my_script.

py results inMy __name__ is: __main__I'm main!Ok.

That’s what we’d expect.

The __name__ was set to __main__ because the file was executed as itself.

Let’s see what happens if we import the file instead.

Start an IPython session in the terminal with python and then importing the file with import my_script (with no .

py extension).

This results in the following output:My __name__ is: my_scriptI'm imported!Wild!.When the file is imported the __name__ becomes the name of the file my_script!.It’s not __main__!And the function that is executed is the one in the else bloc: i_am_imported.

This functionality is useful when you are creating a real module for import into your programs.

It can also be handy when testing.

Read more here.

another_cat = maine_coonWrapHopefully this explanation has made you a bit more comfortable working with Python scripts.

Go play with if __name__ == "__main__": and explain it to someone to solidify your knowledge.

If you’re interested in more tips for Python scripting, check out this article I wrote about argparse.

Learn Enough Python to be Useful: argparseHow to Get Command Line Arguments Into Your Scriptstowardsdatascience.

comI’ve got more articles about jumping out of the Jupyter notebook in the works, so follow me to make sure you don’t miss them!I hope you found this guide helpful.

If you did, please share it on your favorite social media channels so other people can find it too.

????Thanks for reading!.????.. More details

Leave a Reply