Video description
What is the if __name__ condition in Python for?If you've seen __main__
at the end of a Python script and wondered why that is there it is to avoid side effects when importing and allowing you to run the file as a script on the terminal. If you want more details, watch this short video where we work with a Python script to find out those side effects and how the Python script can change once the if condition with __name__
is done at the end.
Now that you know about the conditional at the end, you might want to explore writing Python scripts that use this trick. There are several examples you can follow in these GitHub repositories with argparse
, the Click framework and using sys.argv
:
Table of contents
Product information
- Title: What is __name__ for in Python
- Author(s):
- Release date: February 2022
- Publisher(s): Pragmatic AI Solutions
- ISBN: 50130VIDEOPAIML
You might also like
book
Python One-Liners
Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality …
video
Three Most Important Files in a Python Project
Three Most Important Files in a Python Project Short and Sweet Learn about the three most …
video
Python Dictionaries Course
Python Dictionaries Course Learn how to use Python dictionaries to store and retrieve data Although dictionaries …
video
Beyond "def": Exploring Python functions
See how Python functions work behind the scenes and how you can use them in different …