{"id":3186,"date":"2025-06-01T16:00:22","date_gmt":"2025-06-04T08:39:21","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3186"},"modified":"2025-06-04T16:00:22","modified_gmt":"2025-06-04T08:39:21","slug":"%e4%ba%86%e8%a7%a3python%e7%9a%84name%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e7%9a%84name%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `__name__` \u5c6c\u6027\uff1a\u7528\u6cd5\u8207\u6700\u4f73\u5be6\u8e10 (2025 \u6700\u65b0\u5167\u5bb9)"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __name__\"><\/p>\n<h2>Python \u4e2d\u7684 `__name__` \u5c6c\u6027<\/h2>\n<p>\u5728 Python \u4e2d\uff0c`__name__` \u662f\u4e00\u500b\u91cd\u8981\u7684\u5167\u5efa\u5c6c\u6027\uff0c\u5b83\u7528\u4f86\u53d6\u5f97\u7269\u4ef6\u7684\u540d\u7a31\u3002\u9019\u500b\u5c6c\u6027\u4e0d\u50c5\u9069\u7528\u65bc\u51fd\u6578\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u6a21\u7d44\u3001\u985e\u5225\u548c\u5176\u4ed6\u7269\u4ef6\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5728\u8abf\u8a66\u548c\u932f\u8aa4\u6392\u9664\u6642\u4e86\u89e3\u7269\u4ef6\u7684\u4f86\u6e90\u3002<\/p>\n<p><!--more--><\/p>\n<p>### `__name__` \u5c6c\u6027\u7684\u8a9e\u6cd5<\/p>\n<p>`__name__` \u5c6c\u6027\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nobject.__name__<br \/>\n&#8220;`<\/p>\n<p>\u5176\u4e2d\uff0c`object` \u662f\u8981\u53d6\u5f97\u540d\u7a31\u7684\u7269\u4ef6\u3002<\/p>\n<p>### \u4f7f\u7528\u7bc4\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u7bc4\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 `__name__` \u5c6c\u6027\u4f86\u53d6\u5f97\u51fd\u6578\u7684\u540d\u7a31\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndef add(x, y):<br \/>\n    return x + y<\/p>\n<p>print(add.__name__)  # \u8f38\u51fa\u7d50\u679c\uff1aadd<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba `add` \u7684\u51fd\u6578\uff0c\u7136\u5f8c\u901a\u904e `__name__` \u5c6c\u6027\u4f86\u53d6\u5f97\u51fd\u6578\u7684\u540d\u7a31\uff0c\u8f38\u51fa\u7d50\u679c\u70ba `add`\u3002<\/p>\n<p>### \u53d6\u5f97\u6a21\u7d44\u540d\u7a31<\/p>\n<p>`__name__` \u5c6c\u6027\u540c\u6a23\u53ef\u4ee5\u7528\u65bc\u6a21\u7d44\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport math<\/p>\n<p>print(math.__name__)  # \u8f38\u51fa\u7d50\u679c\uff1amath<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5f15\u5165\u4e86 `math` \u6a21\u7d44\uff0c\u7136\u5f8c\u4f7f\u7528 `__name__` \u5c6c\u6027\u53d6\u5f97\u6a21\u7d44\u7684\u540d\u7a31\uff0c\u8f38\u51fa\u7d50\u679c\u70ba `math`\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664\u8207\u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u4f7f\u7528 `__name__` \u5c6c\u6027\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5728\u9032\u884c\u932f\u8aa4\u6392\u9664\u6642\uff0c\u5feb\u901f\u4e86\u89e3\u662f\u54ea\u4e00\u500b\u51fd\u6578\u6216\u6a21\u7d44\u767c\u751f\u4e86\u932f\u8aa4\u3002\u4f8b\u5982\uff0c\u7576\u4e00\u500b\u51fd\u6578\u5831\u932f\u6642\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 `print()` \u8a9e\u53e5\u4f86\u986f\u793a\u51fd\u6578\u7684\u540d\u7a31\uff0c\u9019\u6a23\u80fd\u5920\u66f4\u5feb\u901f\u5730\u5b9a\u4f4d\u554f\u984c\u3002<\/p>\n<p>\u6b64\u5916\uff0c`__name__` \u5c6c\u6027\u4e5f\u53ef\u4ee5\u7528\u65bc\u689d\u4ef6\u8a9e\u53e5\u4f86\u63a7\u5236\u6a21\u7d44\u7684\u57f7\u884c\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nif __name__ == &#8220;__main__&#8221;:<br \/>\n    # \u9019\u6bb5\u4ee3\u78bc\u53ea\u5728\u6b64\u6a21\u7d44\u88ab\u76f4\u63a5\u57f7\u884c\u6642\u57f7\u884c<br \/>\n    print(&#8220;This module is being run directly&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u5728\u6a21\u7d44\u8a2d\u8a08\u4e2d\u662f\u4e00\u500b\u6700\u4f73\u5be6\u8e10\uff0c\u53ef\u4ee5\u78ba\u4fdd\u6a21\u7d44\u5728\u88ab\u5c0e\u5165\u6642\u4e0d\u6703\u57f7\u884c\u4e0d\u5fc5\u8981\u7684\u4ee3\u78bc\u3002<\/p>\n<p>### \u7e3d\u7d50<\/p>\n<p>Python \u4e2d\u7684 `__name__` \u5c6c\u6027\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u7372\u53d6\u7269\u4ef6\u7684\u540d\u7a31\uff0c\u4e26\u5728\u8abf\u8a66\u6642\u63d0\u4f9b\u91cd\u8981\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u7121\u8ad6\u662f\u51fd\u6578\u3001\u6a21\u7d44\u9084\u662f\u985e\u5225\uff0c`__name__` \u90fd\u662f\u7406\u89e3\u548c\u4f7f\u7528 Python \u7684\u4e00\u500b\u95dc\u9375\u90e8\u5206\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a Python \u7684\u9ad8\u7d1a\u7528\u6cd5\uff0c\u8acb\u53c3\u8003 [\u9019\u88e1](https:\/\/vocus.cc)\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `__name__` \u548c `name()` \u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA1: `__name__` \u662f\u4e00\u500b\u5c6c\u6027\uff0c\u7528\u65bc\u7372\u53d6\u7269\u4ef6\u7684\u540d\u7a31\uff0c\u800c `name()` \u662f\u4e00\u500b\u51fd\u6578\uff0c\u901a\u5e38\u7528\u65bc\u7279\u5b9a\u985e\u578b\u7684\u7269\u4ef6\uff08\u5982\u67d0\u4e9b\u5eab\u6216\u6846\u67b6\uff09\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5728\u6a21\u7d44\u4e2d\u4f7f\u7528 `__name__` \u5c6c\u6027\uff1f**<br \/>\nA2: \u5728\u6a21\u7d44\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 `if __name__ == &#8220;__main__&#8221;:` \u4f86\u6aa2\u67e5\u6a21\u7d44\u662f\u5426\u662f\u76f4\u63a5\u57f7\u884c\uff0c\u4ee5\u6b64\u63a7\u5236\u6a21\u7d44\u7684\u884c\u70ba\u3002<\/p>\n<p>**Q3: `__name__` \u5c6c\u6027\u5728\u8abf\u8a66\u4e2d\u6709\u4ec0\u9ebc\u7528\u9014\uff1f**<br \/>\nA3: \u5728\u8abf\u8a66\u4e2d\uff0c\u4f7f\u7528 `__name__` \u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u8b58\u5225\u7576\u524d\u6b63\u5728\u57f7\u884c\u7684\u51fd\u6578\u6216\u6a21\u7d44\uff0c\u5f9e\u800c\u66f4\u5feb\u5730\u5b9a\u4f4d\u554f\u984c\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>name()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u7528\u65bc\u6aa2\u7d22\u5c0d\u8c61\u7684\u540d\u7a31\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39name()\u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4ee5\u53ca\u5b83\u5982\u4f55\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7ba1\u7406\u5c0d\u8c61\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3186","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Po","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3186"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3186\/revisions"}],"predecessor-version":[{"id":3187,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3186\/revisions\/3187"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}