[Basics of python basics] Why do __name__ == "__main__"

sample.py


import ~~
def hoge
	#~~
if __name__ = "__main__": 
	#~~

I see this kind of code, but what is `` `name```?

There are some special variables that the python interpreter defines before executing code, one of which is the `__name__``` variable. And if the module you are reading, the source code, is the main program, python will set the __name__``` variable to the value `` " main ". If the file is imported from another module, `` `__name__ will contain the module name.

That is, on the console

python sample.py

If you do, it means that the `__name__``` variable is set to `main``` in sample.py that is running as the main program.

In sample.py above, we set these special first defined variables, then execute the import declarations, then `` `defEvaluate the block and create a variable that references a function object called hogehoe. Then go to the if``` statement and perform the processing in it. By this procedure, the processing in if can be executed with the defined function or the imported module.

The meaning of having this is that I don't want it to be executed when it is imported from another module, but when I run it as the main program, I do it and process it. It's the same as how to think of the main function of C ++ or java.

I see.

Recommended Posts

[Basics of python basics] Why do __name__ == "__main__"
Basics of Python ①
Basics of python ①
Basics of Python scraping basics
# 4 [python] Basics of functions
Basics of python: Output
Python "if __name__ == ‘__main__’: ”means
python: Basics of using scikit-learn ①
How much do you know the basics of Python?
Basics of Python × GIS (Part 1)
Why do you add a main ()-if statement in Python?
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
Review of the basics of Python (FizzBuzz)
Basics of Python x GIS (Part 2)
About the basics list of Python basics
Learn the basics of Python ① Beginners
Python basics ⑤
Python basics
Python basics ④
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
[Learning memo] Basics of class by python
[Python3] Understand the basics of Beautiful Soup
I didn't know the basics of Python
The basics of running NoxPlayer in Python
Set the process name of the Python program
[Python] Chapter 02-04 Basics of Python Program (About Comments)
[Python] Chapter 02-03 Basics of Python programs (input / output)
[Introduction to Data Scientists] Basics of Python ♬
[Python3] Understand the basics of file operations
Introduction of Python
Python basics memorandum
[Python] Get the main topics of Yahoo News
Why the Python implementation of ISUCON 5 used Bottle
#Python basics (#matplotlib)
[Python of Hikari-] Chapter 05-06 Control Syntax (Basics of Comprehension)
Python CGI basics
Python slice basics
#Python basics (scope)
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
#Python basics (#Numpy 1/2)
Copy of python
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python array basics
Python profiling basics
[Python] Chapter 02-02 Basics of Python programs (Handling of character strings)
Contents of __name__
Python #Numpy basics
[Python for Hikari] Chapter 09-01 Classes (Basics of Objects)
[Python] Chapter 02-05 Basics of Python programs (string operations / methods)
Python basics: functions
#Python basics (class)