[Python] if __name__ == What is'__main__' :?

[Python] if \ _ \ _ name \ _ \ _ =='\ _ \ _ main \ _ \ _': What is it?

About the role and processing of ʻif name =='main':` described in the .py file.

what are you doing

-** When importing a file, do not execute the following if. ** **

By default, when you import a .py file, the contents of the file are executed.

ʻIf name =='main': `By writing below, you can avoid execution at the time of import.

principle

Uses the property that the variable __name__ behaves differently when imported and when the file is executed.

「__name__」

-** If imported, it will be replaced with "module name" . - If the file is executed, it will be replaced with "main" **.

Only when the file is executed, the condition of the if statement becomes True, so the contents are executed.

test.py


print(__name__)

#output
__main__

Practical example

How to write a file that is not output by import but is output by file execution.

module.py


def function name:
processing

if __name__ == "__main__":
Function name()

▼ Example

hello.py


def hello():
    print("hello world")

if __name__ == "__main__":
    hello()

① In the case of ʻimport hello`

--No output. -The module name "" hello "" is assigned to \ _ \ _ name \ _ \ _

② In the case of $ python hello.py

--Output "hello world" -"\ _ \ _ Main \ _ \ _" is assigned to \ _ \ _ name \ _ \ _

Recommended Posts

[Python] if __name__ == What is'__main__' :?
Python "if __name__ == ‘__main__’: ”means
if True> = False: Python "True" Go "What ???"
Python if statement
What is python
[Python] if statement
What is Python
[Python] What is Pipeline ...
Name identification using python
[Python] What is virtualenv
Python basic if statement
[Python beginner] If __name__ == Move your hand to understand'__main__'.
What to do when "cannot import name xxx" [Python]
[Introduction to Python] What is the important "if __name__ =='__main__':" when dealing with modules?
Addition with Python if statement
[Python] Python and security-① What is Python?
[Python] * args ** What is kwrgs?
What I learned in Python
What is a python map?
Python Basic Course (1 What is Python)
What to do if you can't install pyaudio with pip #Python
What to do if you get a minus zero in Python
What to do if python says "fatal error:'stdio.h' file not found"
What to do if Insecure Platform Warning appears when running Python
What to do if PyAudio cannot be installed on Python 3.7, 3.8, 3.9 on Windows
What is Python? What is it used for?
[Python] What is a zip function?
[Python] What is a with statement?
If python on mac goes missing
Summary if using AWS Lambda (Python)
[Python] What is @? (About the decorator)
What was surprising about Python classes
[Python] What are @classmethods and decorators?
[python] What is the sorted key?
Create python directory Support if directory exists
Python for statement ~ What is iterable ~
What to do with PYTHON release?
[python] Correct usage of if statement
What is the python underscore (_) for?
If Python 3.5.0 installation fails on Mac
Python> What is an extended slice?
Get the host name in Python
[Python] File operation using if statement
Python | What you can do with Python
What to do if there is a decimal in python json .dumps
What to do if you couldn't send an email to Yahoo with Python.
What to do if you can't use scikit grid search in Python
What to do if ipython and python start up with different versions
If you are told cannot by Python import, review the file name
What to do if No Python documentation found for ... appears in pydoc
What to do if the server doesn't start with python manage.py runserver
What happens if you do "import A, B as C" in Python?