{"id":5104,"date":"2024-12-31T15:48:35","date_gmt":"2024-12-31T06:48:35","guid":{"rendered":"https:\/\/yorozu.cloudfree.jp\/wordpress\/?p=5104"},"modified":"2025-03-28T11:13:31","modified_gmt":"2025-03-28T02:13:31","slug":"5104","status":"publish","type":"post","link":"https:\/\/yorozu.cloudfree.jp\/wordpress\/?p=5104","title":{"rendered":"if __name__ == \u201c__main__\u201d\u00a0"},"content":{"rendered":"\n<p>\u5b9f\u884c\u4f8b\u306f\u300cstupa.sarnath\u300d\u306e\u300cGoogle Colabotry\u300d\u74b0\u5883\u306b\u4fdd\u5b58\u3057\u3066\u3042\u308b<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\uff11\uff0eif __name__ == &#8220;__main__&#8221;\u306e\u610f\u5473<\/strong><\/h4>\n\n\n\n<p>\u3000\u30a2\u30f3\u30c0\u30fc\u30b9\u30b3\u30a2\u306b\u60d1\u308f\u3055\u308c\u305a\u306b\u8aad\u3081\u3070<br>\u3000\u3000\u3000\u300c<code>__name__<\/code>\u306b\u683c\u7d0d\u3055\u308c\u3066\u3044\u308b\u5024\u304c<code>'__main__'<\/code>\u3068\u3044\u3046\u6587\u5b57\u5217\u3067\u3042\u308c\u3070<br>\u3000\u3000\u3000\u3000\u3000\u3053\u306e\u6761\u4ef6\u6587\u4ee5\u964d\u306e\u51e6\u7406\u3092\u5b9f\u884c\u3059\u308b\u3068\u3044\u3046\u5358\u306a\u308bif\u6587<br><br>\u3000Python\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306b\u304a\u307e\u3058\u306a\u3044\u306e\u3088\u3046\u306b\u66f8\u304b\u308c\u3066\u3044\u308b<br>\u3000\u3000\u3000\u3053\u306eif\u6587\u306e\u610f\u5473\u3068\u4f7f\u3044\u65b9\u3092\u8aac\u660e\u3059\u308b<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\uff12\uff0e\u4f7f\u7528\u4f8b<\/strong><\/h4>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># mymodule.py\nprint(&quot;mymodule\u304c\u8d77\u52d5\u3055\u308c\u307e\u3057\u305f&quot;)\n\n# then\u306e\u90e8\u5206\u306f\u76f4\u63a5\u8d77\u52d5\u6642\u3001else\u306e\u90e8\u5206\u306fimport\u3055\u308c\u305f\u6642\nif __name__ == &quot;__main__&quot;:\n    print(&quot;\u3053\u306e\u30b3\u30fc\u30c9\u306fmymodule\u3092\u76f4\u63a5\u5b9f\u884c\u3057\u305f\u3068\u304d\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059&quot;,__name__)\nelse:\n\u3000\u3000print(&quot;\u3053\u306e\u30b3\u30fc\u30c9\u306fmymodule\u3092import\u3057\u305f\u3068\u304d\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059&quot;,__name__)\n<\/code><\/pre><\/div>\n\n\n\n<p>\u3000\u4e0a\u8a18\u30b3\u30fc\u30c9\u306e\u8aac\u660e<br>\u3000\u3000\u3000<code>import mymodule<\/code>\u306e\u5834\u5408<br>\u3000\u3000\u3000\u3000\u3000\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u90e8\u5206\u304c\u51e6\u7406\u3055\u308c\u4ee5\u4e0b\u306e\u8868\u793a\u304c\u3055\u308c\u308b<br>\u3000\u3000\u3000\u3000\u3000\u3000\u3000&#8221;mymodule\u304c\u8d77\u52d5\u3055\u308c\u307e\u3057\u305f&#8221;<br>\u3000\u3000\u3000\u3000\u3000<code><strong>if __name__ == \u201c__main__\u201d:<\/strong><\/code>\u306e<br>\u3000\u3000\u3000\u3000\u3000\u3000\u3000<code><strong>__name__ <\/strong><\/code>\u306f\u300cmain\u300d\u306a\u306e\u3067else\u51e6\u7406\u304c\u5b9f\u884c\u3055\u308c\u308b<br>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u201d\u3053\u306e\u30b3\u30fc\u30c9\u306fmymodule\u3092import\u3057\u305f\u3068\u304d\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059&#8221;<br><br>\u3000\u3000\u3000mymodule\u3092\u76f4\u63a5\u5b9f\u884c\uff08mymodule.py\u306e\u8d77\u52d5\uff09\u3057\u305f\u5834\u5408<br>\u3000\u3000\u3000\u3000\u3000\u30c8\u30c3\u30d7\u30ec\u30d9\u30eb\u306e\u90e8\u5206\u304c\u51e6\u7406\u3055\u308c\u4ee5\u4e0b\u306e\u8868\u793a\u304c\u3055\u308c\u308b<br>\u3000\u3000\u3000\u3000\u3000\u3000\u3000&#8221;mymodule\u304c\u8d77\u52d5\u3055\u308c\u307e\u3057\u305f&#8221;<br>\u3000\u3000\u3000\u3000\u3000<strong>if __name__ == \u201c__main__\u201d<\/strong>:\u306e<br>\u3000\u3000\u3000\u3000\u3000\u3000\u3000<code><strong>__name__ <\/strong><\/code>\u306f\u300c<code><strong>__main__<\/strong><\/code>\u300d\u306a\u306e\u3067then\u51e6\u7406\u304c\u5b9f\u884c\u3055\u308c\u308b<br>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000&#8221;\u3053\u306e\u30b3\u30fc\u30c9\u306fmymodule\u3092\u76f4\u63a5\u5b9f\u884c\u3057\u305f\u3068\u304d\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059&#8221;<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># calculator.py\ndef add(x, y):\n    return x + y\n\ndef subtract(x, y):\n    return x - y\n\nif __name__ == &quot;__main__&quot;:\n    print(add(3, 4))\n    print(subtract(10, 5))<\/code><\/pre><\/div>\n\n\n\n<p>\u3000\u4e0a\u8a18\u30b3\u30fc\u30c9\u306e\u8aac\u660e<br>\u3000\u3000\u3000\u76f4\u63a5\u5b9f\u884c\u3059\u308b\u3068\u3001\u8db3\u3057\u7b97\u3068\u5f15\u304d\u7b97\u306e\u7d50\u679c\u304c\u51fa\u529b\u3055\u308c\u308b<br><br>\u3000\u4e0a\u8a18\u30b3\u30fc\u30c9\u306e\u5b9f\u884c\u7d50\u679c<br>\u3000\u3000\u3000mymodule\u304c<code>\u8d77\u52d5\u3055\u308c\u307e\u3057\u305f<\/code><br>\u3000\u3000\u3000<code>\u3053\u306e\u30b3\u30fc\u30c9\u306fmymodule\u3092\u76f4\u63a5\u5b9f\u884c\u3057\u305f\u3068\u304d\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059<\/code><br>\u3000\u3000\u3000<code> *** __main__<\/code><br>\u3000\u3000\u3000<code>7<\/code><br>\u3000\u3000\u3000<code>5 <\/code><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># calculator.py\ndef add(x, y):\n    return x + y\n\ndef subtract(x, y):\n    return x - y\n\nif __name__ == &quot;__main__&quot;:\n    print(add(3, 4))\n    print(subtract(10, 5))\n\nimport main\nprin\uff54(add(3,4))<\/code><\/pre><\/div>\n\n\n\n<p>\u4e0a\u8a18\u30b3\u30fc\u30c9\u306e\u8aac\u660e<br>\u3000\u3000\u3000import\u3059\u308b\u3068\u3001\u8db3\u3057\u7b97\u3068\u5f15\u304d\u7b97\u306e\u7d50\u679c\u304c\u51fa\u529b\u3055\u308c\u308b<br><br>\u4e0a\u8a18\u30b3\u30fc\u30c9\u306e\u5b9f\u884c\u7d50\u679c<br>\u3000\u3000\u3000<code>\uff0a\uff0a\uff0a\uff0a\uff0a\uff0a import\u3000\u51e6\u7406<\/code><br>\u3000\u3000\u3000<code>mymodule\u304c<\/code>\u8d77\u52d5\u3055\u308c\u307e\u3057\u305f<br>\u3000\u3000\u3000\u3053\u306e\u30b3\u30fc\u30c9\u306fmymodule\u3092import\u3057\u305f\u3068\u304d\u306b\u5b9f\u884c\u3055\u308c\u307e\u3059<br>\u3000\u3000\u3000+++ main<br>\u3000\u3000\u3000\uff0a\uff0a\uff0a\uff0a\uff0a\uff0a\uff0a<br>\u3000\u3000\u3000import\u3000\u51e6\u7406<br>\u3000\u3000\u30007<br>\u3000\u3000\u30007<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30fb<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[17],"class_list":["post-5104","post","type-post","status-publish","format-standard","hentry","category-99_","tag-python"],"_links":{"self":[{"href":"https:\/\/yorozu.cloudfree.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/5104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yorozu.cloudfree.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yorozu.cloudfree.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yorozu.cloudfree.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/yorozu.cloudfree.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5104"}],"version-history":[{"count":16,"href":"https:\/\/yorozu.cloudfree.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/5104\/revisions"}],"predecessor-version":[{"id":5774,"href":"https:\/\/yorozu.cloudfree.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/5104\/revisions\/5774"}],"wp:attachment":[{"href":"https:\/\/yorozu.cloudfree.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yorozu.cloudfree.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yorozu.cloudfree.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}