[Python beginner] If __name__ == Move your hand to understand'__main__'.

what will you do

ʻIf \ _ \ _ name \ _ \ _ =='\ _ \ _ main \ _ \ _': ` I experienced how the output changes in 4steps + 2steps.

This time, I will show the behavior when executing .py indirectly with a tool such as "spyder" that is often used by beginners.

If you want to run "$ python XXX.py" directly in the terminal, please see the details from at the bottom of this article.

Practice

__name__

1. First, write a function that print ('Hello!') And save it as "hello.py".

Example 1_hello.Define py


#hello.py
def function():
    print('Hello!')

2 . Try importing "hello.py" with the next new .py

Example 2_hello.import py


import hello
hello.function()

#Output contents
#--------------------
# Hello!
#--------------------

↓ You can see that the function has been executed.

3 Edit \ .hello.py and try to output \ _ \ _ name \ _ \ _ together

Example 3_add name


#hello.py
def function():
    print('Hello!')
    print(' __name__Then, what is displayed..!? -->' ,  __name__)

4 . When I try to import again ...

Example 4_hello.Try running py


import hello
hello.function()

#Output contents
#--------------------
#Hello!
#__name__Then, what is displayed..!? --> hello
#--------------------

↓ In the \ _ \ _ name \ _ \ _ part, "hello" of "hello.py" was displayed!

Conclusion (What is \ _ \ _ name \ _ \ _)

It turns out that \ _ \ _ name \ _ \ _ contains the module name 'hello' of the imported module 'hello.py'.

__main__

Next, edit hello.py and add \ _ \ _ main \ _ \ _.

Example 1_Put "Sato" in the main.


#hello.py
def function(name):
    print('Hello!',name)
    print('By the way__name__The contents are-->',__name__)

if __name__ == '__main__':
    print('Is the function in main executed?..?',function('Sato'))

Try running hello.py.

Example 2_Display "Tanaka"


import hello
hello.function('Tanaka')
#Output contents
#--------------------
#Hello! Tanaka
#By the way__name__The contents are--> hello
#--------------------

↓ \ _ \ _ Name \ _ \ _ remained the module name 'hello' of the module 'hello.py'. Therefore, the contents of ʻif name =='main':` (prepared "Sato") were not executed even though it was in function ().

Conclusion (What is \ _ \ _ main \ _ \ _)

I found that by setting ʻif name =='main': `, the following \ _ \ _ main \ _ \ _ is not executed.

Summary

Write the part that should not be executed when imported under ʻif name =='main': `.

reference [Python] if name == What is'main':?

Recommended Posts

[Python beginner] If __name__ == Move your hand to understand'__main__'.
Move your hand to understand the chi-square distribution
Python "if __name__ == ‘__main__’: ”means
[Python] if __name__ == What is'__main__' :?
If __name__ == Raise your hand, if you write the code under'__main__'
[Supplementary course] If __name__ == If you write the code under'__main__', raise your hand.
Directory move tool to Python package (pycd)
[Python] Introduce UIKit3 to your Django project
Want to add type hints to your Python decorator?
Python hand play (one line notation of if)
List of Python code to move and remember
[Python] Do your best to speed up SQLAlchemy
[Introduction to Python] What is the important "if __name__ =='__main__':" when dealing with modules?