0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pythonの __name__ == "main"の意味

Last updated at Posted at 2024-04-11

__name__ == "__main__"の意味

参考

関数用のファイル「test_m.py」、関数呼び出し用ファイル「call_m.py」

#test_m.py
def sample():
    print(__name__)
    
sample()

#出力結果
__main__
#call_m.py
from test_m import tax
sample()

#出力結果
test_m
test_m

モジュールの呼び出し(import)で出力したときに2回出力するのを防ぐ。

#test_m.py
def sample():
    print(__name__)
    
if __name__ == "__main__":
    print(sample())
#call_m.py
from test_m import tax
sample()

#出力結果
test_m
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?