Why You Must Use Python if main or if name == main block?
This Python tutorial is about Python main function, how to use the if `__name__ == '__main__'` syntax, how it works. Also in this Python if main tutorial I show what is `__name__` variable in Python.
The if `__name__ == '__main__'` block checks the value of the `__name__` variable. Each Python module has it. If a Python script was run as the main script of your project the `__name__` variable get the `__main__` as the value. And this if main condition will be evaluated as True.
If a Python script was imported the `__name__` variable get the name of the file as the value. And this Python if main condition will return False.
Python main() function is just the name of a function. And you can use any function you want. But by convention a Python script should have an entry point, and this entry point is the Python main() function, that should be run if Python module executed as the main module of you project.
Shortly: this Python main function tutorial is about why do you need to use it in every Python project.
Follow me @:
Telegram: https://t.me/red_eyed_coder_club
Twitter: / codereyed
Facebook: https://fb.me/redeyedcoderclub
The short Python tutorial series in 1 minute.
** Other Python shorts **
1. Python if main function: • Why You Must Use Python if main, if n...
2. Python else with loops: • Python Else with FOR and WHILE loops,...
3. Simplest way to write to file: • Simplest way to write files in Python...
4. Simplest way to delete file: • How to delete files in Python with Pa...
➥➥➥ SUBSCRIBE FOR MORE VIDEOS ➥➥➥
Red Eyed Coder Club is the best place to learn Python programming and Django:
Subscribe ⇢ / @redeyedcoderclub
Why You Must Use Python if main or if name == main block?
• Why You Must Use Python if main, if n...
#python #pythonshorts #redeyedcoderclub