{"id":367,"date":"2025-05-26T17:35:46","date_gmt":"2025-05-26T09:35:46","guid":{"rendered":"https:\/\/cn1.club\/?p=367"},"modified":"2025-05-26T18:15:17","modified_gmt":"2025-05-26T10:15:17","slug":"python-%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%9a-%e7%90%86%e8%a7%a3%e5%86%85%e7%bd%ae%e5%8f%98%e9%87%8f__name__","status":"publish","type":"post","link":"https:\/\/cn1.club\/?p=367","title":{"rendered":"Python \u5b66\u4e60\u7b14\u8bb0\uff1a \u7406\u89e3\u5185\u7f6e\u53d8\u91cf:__name__"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"606\" height=\"342\" src=\"http:\/\/cn1.club\/wp-content\/uploads\/2025\/05\/1748252095-u18540495913469181297fm253fmtautoapp138fJPEG.webp\" alt=\"\" class=\"wp-image-368\" srcset=\"https:\/\/cn1.club\/wp-content\/uploads\/2025\/05\/1748252095-u18540495913469181297fm253fmtautoapp138fJPEG.webp 606w, https:\/\/cn1.club\/wp-content\/uploads\/2025\/05\/1748252095-u18540495913469181297fm253fmtautoapp138fJPEG-300x169.webp 300w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/figure><\/div>\n\n\n<p>\u5728 Java \u4e2d\uff0c\u7a0b\u5e8f\u7684\u5165\u53e3\u901a\u5e38\u662f\u663e\u5f0f\u58f0\u660e\u7684\uff0c\u4f8b\u5982 Java \u7684 <code class=\"prettyprint\" >main<\/code> \u65b9\u6cd5\u3002\u800c Python \u5e76\u6ca1\u6709\u5f3a\u5236\u8bbe\u5b9a\u201c\u4e3b\u5165\u53e3\u51fd\u6570\u201d\uff0c\u8fd9\u8ba9\u5f88\u591a\u521d\u5b66\u8005\u5728\u770b\u5230 <code class=\"prettyprint\" >if __name__ == &quot;__main__&quot;<\/code> \u8fd9\u53e5\u4ee3\u7801\u65f6\u611f\u5230\u56f0\u60d1\u3002\u672c\u6587\u7ed3\u5408\u5b9e\u9645\u4f8b\u5b50\uff0c\u4ece\u57fa\u7840\u6982\u5ff5\u51fa\u53d1\uff0c\u9010\u6b65\u89e3\u91ca\u8fd9\u53e5\u4ee3\u7801\u7684\u542b\u4e49\u53ca\u5176\u4f7f\u7528\u573a\u666f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-0\">Python \u811a\u672c\u662f\u5982\u4f55\u6267\u884c\u7684\uff1f<\/h2>\n\n\n\n<p>Python \u6587\u4ef6\u5728\u88ab\u6267\u884c\u65f6\uff0c\u89e3\u91ca\u5668\u4f1a\u4ece\u6587\u4ef6\u7684\u6700\u4e0a\u65b9\u5f00\u59cb\uff0c\u9010\u884c\u6267\u884c<strong>\u672a\u7f29\u8fdb\u7684\u4ee3\u7801<\/strong>\uff08\u4e5f\u5c31\u662f\u9876\u5c42\u4ee3\u7801\uff09\u3002\u6bd4\u5982\u4e0b\u9762\u7684\u811a\u672c\uff1a<\/p>\n\n\n\n<p><code class=\"prettyprint\" >print(&quot;Hello, world!&quot;)<\/code><\/p>\n\n\n\n<p>\u76f4\u63a5\u4fdd\u5b58\u4e3a <code class=\"prettyprint\" >hello.py<\/code> \u5e76\u8fd0\u884c\uff0c\u5b83\u4f1a\u7acb\u5373\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code class=\"prettyprint\" >Hello, world!<\/code><\/pre>\n\n\n\n<p>\u8fd9\u8bf4\u660e Python \u6587\u4ef6\u4e0d\u9700\u8981\u5305\u88c5\u5728\u51fd\u6570\u6216\u7c7b\u91cc\uff0c\u4e5f\u80fd\u76f4\u63a5\u8fd0\u884c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-1\">\u4ec0\u4e48\u662f <code class=\"prettyprint\" >__name__<\/code>\uff1f<\/h2>\n\n\n\n<p>\u6bcf\u4e2a Python \u6a21\u5757\uff08<code class=\"prettyprint\" >.py<\/code> \u6587\u4ef6\uff09\u5728\u6267\u884c\u65f6\uff0c\u89e3\u91ca\u5668\u90fd\u4f1a\u81ea\u52a8\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf <code class=\"prettyprint\" >__name__<\/code>\u3002<\/p>\n\n\n\n<p>\u5b83\u7684\u503c\u53d6\u51b3\u4e8e\u8fd9\u4e2a\u6a21\u5757\u7684\u8fd0\u884c\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u8fd0\u884c\u65b9\u5f0f<\/th><th>__name__ \u7684\u503c<\/th><\/tr><\/thead><tbody><tr><td>\u811a\u672c\u88ab\u76f4\u63a5\u8fd0\u884c<\/td><td>\"__main__\"<\/td><\/tr><tr><td>\u811a\u672c\u88ab\u5176\u4ed6\u6a21\u5757\u5bfc\u5165<\/td><td>\u6a21\u5757\u540d\uff08\u4e0d\u5e26 <code class=\"prettyprint\" >.py<\/code>\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u9a8c\u8bc1\u8fd9\u4e00\u70b9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># tool.py<br>print(\"\u5f53\u524d __name__ \u4e3a\uff1a\", __name__)<br><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6267\u884c <code class=\"prettyprint\" >python tool.py<\/code>\uff0c\u8f93\u51fa\u4e3a\uff1a<code class=\"prettyprint\" >\u5f53\u524d __name__ \u4e3a\uff1a __main__<\/code><\/li>\n\n\n\n<li>\u521b\u5efa\u53e6\u4e00\u4e2a\u6587\u4ef6 <code class=\"prettyprint\" >main.py<\/code>\uff0c\u5199\u5165 <code class=\"prettyprint\" >import tool<\/code>\uff0c\u8fd0\u884c\u540e\u8f93\u51fa\u4e3a\uff1a<code class=\"prettyprint\" >\u5f53\u524d __name__ \u4e3a\uff1a tool<\/code><\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u8bf4\u660e <code class=\"prettyprint\" >__name__<\/code> \u7684\u503c\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u662f\u4e0d\u540c\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-2\">\u4e3a\u4ec0\u4e48\u8981\u5199 if __name__ == \"__main__\"\uff1f<\/h2>\n\n\n\n<p>\u8fd9\u53e5\u5224\u65ad\u7684\u76ee\u7684\u662f\uff1a<strong>\u4ec5\u5f53\u5f53\u524d\u6587\u4ef6\u662f\u4f5c\u4e3a\u4e3b\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u624d\u6267\u884c\u67d0\u4e9b\u4ee3\u7801\uff1b\u5982\u679c\u5b83\u88ab\u522b\u7684\u6587\u4ef6\u5bfc\u5165\uff0c\u5219\u4e0d\u6267\u884c\u3002<\/strong><\/p>\n\n\n\n<p>\u6765\u770b\u4e00\u4e2a\u6807\u51c6\u5199\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def main():<br>    print(\"\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\")<br><br>if __name__ == \"__main__\":<br>    main()<br><\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u76f4\u63a5\u8fd0\u884c\u8fd9\u4e2a\u6587\u4ef6\uff0c\u4f1a\u770b\u5230\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c<br><\/pre>\n\n\n\n<p>\u4f46\u5982\u679c\u8fd9\u4e2a\u6587\u4ef6\u88ab\u5176\u4ed6\u811a\u672c\u5bfc\u5165\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># other.py<br>import your_script<br><\/pre>\n\n\n\n<p>\u5c31\u4e0d\u4f1a\u6709\u4efb\u4f55\u8f93\u51fa\u3002\u56e0\u4e3a <code class=\"prettyprint\" >__name__ != &quot;__main__&quot;<\/code>\uff0c\u6240\u4ee5 <code class=\"prettyprint\" >main()<\/code> \u4e0d\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u8fd9\u5728\u5f00\u53d1\u5e93\u6216\u5de5\u5177\u6a21\u5757\u65f6\u975e\u5e38\u6709\u7528\uff0c\u9632\u6b62\u6a21\u5757\u5728\u88ab\u5bfc\u5165\u65f6\u81ea\u52a8\u6267\u884c\u4e00\u4e9b\u6d4b\u8bd5\u4ee3\u7801\u6216\u4e3b\u6d41\u7a0b\u903b\u8f91\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-3\">\u4e00\u4e2a\u66f4\u5b8c\u6574\u7684\u793a\u4f8b\u7ed3\u6784<\/h2>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u8f83\u63a8\u8350\u7684\u811a\u672c\u7ed3\u6784\uff0c\u5b83\u65e2\u9002\u5408\u76f4\u63a5\u8fd0\u884c\uff0c\u4e5f\u65b9\u4fbf\u4ee5\u540e\u5f53\u4f5c\u6a21\u5757\u5f15\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def \u521d\u59cb\u5316():<br>    print(\"\u6b63\u5728\u521d\u59cb\u5316\u8d44\u6e90...\")<br><br>def \u4e3b\u903b\u8f91():<br>    print(\"\u5f00\u59cb\u6267\u884c\u4e3b\u7a0b\u5e8f\u903b\u8f91...\")<br><br>def main():<br>    \u521d\u59cb\u5316()<br>    \u4e3b\u903b\u8f91()<br><br>if __name__ == \"__main__\":<br>    main()<br><\/pre>\n\n\n\n<p>\u7ed3\u6784\u6e05\u6670\u3001\u903b\u8f91\u660e\u786e\uff0c\u662f\u5b9e\u9645\u5f00\u53d1\u4e2d\u975e\u5e38\u5e38\u89c1\u7684\u7ec4\u7ec7\u65b9\u5f0f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-4\">\u5982\u679c\u4e00\u4e2a\u6587\u4ef6\u91cc\u53ea\u6709\u51fd\u6570\uff0c\u6ca1\u6709\u5165\u53e3\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/h2>\n\n\n\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def say_hello():<br>    print(\"Hello!\")<br><\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u4fdd\u5b58\u4e3a <code class=\"prettyprint\" >test.py<\/code> \u5e76\u76f4\u63a5\u8fd0\u884c\u5b83\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python test.py<br><\/pre>\n\n\n\n<p>\u4f1a\u53d1\u73b0\u4ec0\u4e48\u90fd\u4e0d\u4f1a\u8f93\u51fa\u3002\u539f\u56e0\u5f88\u7b80\u5355\uff1aPython \u53ea\u662f<strong>\u5b9a\u4e49\u4e86\u51fd\u6570<\/strong>\uff0c\u5e76\u6ca1\u6709\u8c03\u7528\u5b83\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e5f\u6b63\u662f\u4e3a\u4ec0\u4e48\u63a8\u8350\u4f7f\u7528 <code class=\"prettyprint\" >main()<\/code> \u51fd\u6570\u5e76\u5728 <code class=\"prettyprint\" >if __name__ == &quot;__main__&quot;<\/code> \u4e2d\u8c03\u7528\u5b83\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-5\">\u5c0f\u7ed3<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python \u7684\u9876\u5c42\u4ee3\u7801\u4f1a\u5728\u6587\u4ef6\u88ab\u8fd0\u884c\u65f6\u7acb\u5373\u6267\u884c\uff1b<\/li>\n\n\n\n<li><code class=\"prettyprint\" >__name__<\/code> \u662f Python \u81ea\u52a8\u8d4b\u503c\u7684\u5185\u7f6e\u53d8\u91cf\uff1b<\/li>\n\n\n\n<li>\u5f53\u811a\u672c\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c<code class=\"prettyprint\" >__name__ == &quot;__main__&quot;<\/code>\uff1b<\/li>\n\n\n\n<li>\u5f53\u811a\u672c\u88ab\u5176\u4ed6\u6a21\u5757\u5bfc\u5165\u65f6\uff0c<code class=\"prettyprint\" >__name__<\/code> \u7b49\u4e8e\u6a21\u5757\u540d\uff1b<\/li>\n\n\n\n<li>\u4f7f\u7528 <code class=\"prettyprint\" >if __name__ == &quot;__main__&quot;<\/code> \u53ef\u4ee5\u907f\u514d\u5728\u5bfc\u5165\u6a21\u5757\u65f6\u6267\u884c\u4e3b\u903b\u8f91\u4ee3\u7801\uff1b<\/li>\n\n\n\n<li>\u5efa\u8bae\u5c06\u4e3b\u6d41\u7a0b\u5199\u5165 <code class=\"prettyprint\" >main()<\/code> \u51fd\u6570\u4e2d\uff0c\u518d\u901a\u8fc7\u8fd9\u53e5\u5224\u65ad\u6765\u51b3\u5b9a\u662f\u5426\u8c03\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e0d\u4ec5\u8ba9\u4ee3\u7801\u66f4\u6e05\u6670\uff0c\u4e5f\u4e3a\u6a21\u5757\u5316\u5f00\u53d1\u3001\u5355\u5143\u6d4b\u8bd5\u63d0\u4f9b\u4e86\u826f\u597d\u7684\u57fa\u7840\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Java \u4e2d\uff0c\u7a0b\u5e8f\u7684\u5165\u53e3\u901a\u5e38\u662f\u663e\u5f0f\u58f0\u660e\u7684\uff0c\u4f8b\u5982 Java \u7684 main \u65b9\u6cd5\u3002\u800c Python \u5e76\u6ca1\u6709\u5f3a\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":368,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"class_list":["post-367","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-xuexi"],"_links":{"self":[{"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/posts\/367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=367"}],"version-history":[{"count":0,"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/posts\/367\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/media\/368"}],"wp:attachment":[{"href":"https:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}