python notes: Modularization: __name__ == How to use'__main__'

Motivation

Often seen in python scripts,

pseudo.py


if __name__ == '__main__':

I didn't know what it was.

modularization

I used this as a reference. http://programming-study.com/technology/python-if-main/

In python, you often pull and use something defined in other scripts. Import the library. It is useful when you want to make a function you defined yourself into a separate script and import it as a module (although it is not possible now).

\ _ \ _ Name \ _ \ _ in the above if is a variable that is automatically created when the script is executed, and when the script is executed directly, \ _ \ _ name \ _ \ _ is \ _ \ _ main \ _ The name \ _ is stored.

In other words, the above if statement branches depending on whether the script was executed directly from the shell.

As shown below, I wrote a script that has executable statements inside and outside the if statement.

name_is_main.py


def func(a,b):
  return a + b

print('MY NAME IS ' + __name__)

if __name__ == '__main__':
  print('hello!')
  print(func(3,4))

Execution result: MY NAME IS __main__ hello! 7

name_is__not_main.py


import name_is_main as nim

print('my name is' + __name__)

if __name__ == '__main__':
  print('world!')
  print(nim.func(7,8))

Execution result: MY NAME IS name_is_main my name is __main__ world! 15

In name \ _is \ _main.py, \ _ \ _ main \ _ \ _ was entered in \ _ \ _ name \ _ \ _ as it was, and the contents of the if statement were also executed.

In name \ _is \ _ not \ _main.py, the print statement outside the if statement of the imported name \ _is \ _main.py was also executed. In the imported name \ _is \ _main.py, the script name name \ _is \ _main is stored in \ _ \ _ name \ _ \ _. On the other hand, \ _ \ _ main \ _ \ _ was stored in \ _ \ _ name \ _ \ _ of name \ _is \ _ not \ _main.py itself.

As mentioned above, if there is an extra description in the script to be imported, it will be executed without permission on the importing side.

use?

In the site that I referred to, it was introduced to write a script to test the operation of the module as a usage of if \ _ \ _ name \ _ \ _ == \ _ \ _ main \ _ \ _ :. It was. I see.

How is it used outside of testing?

When you're writing a script that you don't intend to modularize, what do you guys do if you get "I think this function can be used elsewhere, so think about modularization ..."? At that point, do you move the function to another script, or do you decide to use an if statement like the one above and leave it in the script as is?

Recommended Posts

python notes: Modularization: __name__ == How to use'__main__'
How to install Python
How to install python
[2020.8 latest] How to install Python
How to get the variable name itself in python
How to install Python [Windows]
python3: How to use bottle (2)
Python notes to forget soon
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[Hyperledger Iroha] Notes on how to use the Python SDK
How to do Bulk Update with PyMySQL and notes [Python]
How to install python using anaconda
How to write a Python class
[Python] How to FFT mp3 data
Python: How to use async with
[Python] How to use Pandas Series
How to collect images in Python
How to use Requests (Python Library)
How to use SQLite in Python
[Introduction to Python] How to parse JSON
Notes on how to use pywinauto
How to get the Python version
Notes on how to use featuretools
How to get started with Python
[Python] How to import the library
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
[Python] How to swap array values
How to wrap C in Python
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to speed up Python calculations
How to calculate date with python
How to access wikipedia from python
How to use python zip function
[Nanonets] How to post Memo [Python]
Notes on how to use doctest
Notes on how to write requirements.txt
How to handle Japanese in Python
[Python] How to use Typetalk API
How to package and distribute Python scripts
[Introduction to Python] How to use class in Python?
Qiita (1) How to write a code name
How to read pydoc on python interpreter