LoginSignup
2
2

More than 5 years have passed since last update.

__name__の中身

Posted at

__name__に入る値は下記のようになります。


実行する構成
.
├── entry_point.py
└── test_module.py

entry_point.py
# coding: utf-8

if __name__ == "__main__":
    # エントリポイントとして呼ばれた場合には
    # __name__には"__main__"文字列が入る

    # ex) python entry_point.py
    print "__main__!!"
else:
    # 他からimportなどmoduleとして呼ばれた場合には
    # __name__にはモジュール名が入る

    # ex) python test_module.py
    # __name__ == "entry_point"
    print __name__
test_module.py
import entry_point

entry_point.pyを実行した場合
% python entry_point.py
__main__!!

test_module.pyを実行した場合
% python test_module.py
entry_point


実行方法により入る値が変わるようです.

2
2
0

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
2
2