{"id":3382,"date":"2025-05-29T15:46:40","date_gmt":"2025-06-04T08:01:39","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3382"},"modified":"2025-06-04T15:46:40","modified_gmt":"2025-06-04T08:01:39","slug":"%e4%ba%86%e8%a7%a3python%e7%9a%84intern%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e7%9a%84intern%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `intern()` \u51fd\u6578\uff1a\u6700\u4f73\u5be6\u8e10\u8207\u61c9\u7528\u793a\u4f8b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, intern, \u5b57\u4e32\u6c60, \u8a18\u61b6\u9ad4\u7ba1\u7406, \u5b57\u4e32\u6bd4\u8f03\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 `intern()` \u51fd\u6578<\/h1>\n<p>Python \u7684 `intern()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\uff0c\u5b83\u80fd\u5920\u5c07\u5b57\u4e32\u5b58\u5165 Python \u7684\u5b57\u4e32\u6c60\u4e2d\uff0c\u8b93\u5f8c\u7e8c\u7684\u64cd\u4f5c\u4e2d\u80fd\u5feb\u901f\u67e5\u627e\u548c\u4f7f\u7528\u76f8\u540c\u7684\u5b57\u4e32\u3002\u9019\u5728\u6bd4\u8f03\u5b57\u4e32\u6642\u5c24\u5176\u6709\u6548\uff0c\u56e0\u70ba\u5b83\u80fd\u5920\u63d0\u9ad8\u6548\u80fd\u4e26\u6e1b\u5c11\u8a18\u61b6\u9ad4\u7684\u4f7f\u7528\u3002<\/p>\n<p>## `intern()` \u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5<\/p>\n<p>`intern()` \u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\ns = intern(string)<br \/>\n&#8220;`<\/p>\n<p>\u5176\u4e2d\uff0c`string` \u662f\u8981\u5b58\u5165\u5b57\u4e32\u6c60\u4e2d\u7684\u5b57\u4e32\uff0c\u800c `s` \u5247\u662f\u5b58\u5165\u5b57\u4e32\u6c60\u5f8c\u7684\u5b57\u4e32\u5c0d\u8c61\u3002<\/p>\n<p>## `intern()` \u7684\u4e3b\u8981\u529f\u80fd\u8207\u61c9\u7528<\/p>\n<p>`intern()` \u51fd\u6578\u7684\u4e3b\u8981\u529f\u80fd\u662f\u5c07\u5b57\u4e32\u5b58\u5165\u5b57\u4e32\u6c60\u4e2d\uff0c\u5f9e\u800c\u5728\u4e4b\u5f8c\u7684\u64cd\u4f5c\u4e2d\u53ef\u4ee5\u5feb\u901f\u6bd4\u8f03\u5b57\u4e32\u3002\u4f8b\u5982\uff0c\u7576\u6211\u5011\u9700\u8981\u5224\u65b7\u5169\u500b\u5b57\u4e32\u662f\u5426\u76f8\u7b49\u6642\uff0c\u82e5\u5c07\u5b83\u5011\u90fd\u5b58\u5165\u5b57\u4e32\u6c60\u4e2d\uff0c\u53ef\u4ee5\u901a\u904e\u6aa2\u67e5\u5b83\u5011\u662f\u5426\u6307\u5411\u76f8\u540c\u7684\u5b57\u4e32\u5c0d\u8c61\u4f86\u5feb\u901f\u5224\u65b7\u3002<\/p>\n<p>### \u793a\u4f8b\u7a0b\u5f0f\u78bc<\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 `intern()` \u51fd\u6578\u7684\u793a\u4f8b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom sys import intern<\/p>\n<p>s1 = &#8220;Hello&#8221;<br \/>\ns2 = &#8220;Hello&#8221;<\/p>\n<p>s1 = intern(s1)<br \/>\ns2 = intern(s2)<\/p>\n<p>if s1 is s2:<br \/>\n    print(&#8220;s1 \u548c s2 \u662f\u76f8\u7b49\u7684&#8221;)<br \/>\nelse:<br \/>\n    print(&#8220;s1 \u548c s2 \u4e0d\u662f\u76f8\u7b49\u7684&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5c07\u5169\u500b\u5b57\u4e32 `s1` \u548c `s2` \u5b58\u5165\u5b57\u4e32\u6c60\u4e2d\uff0c\u7136\u5f8c\u6bd4\u8f03\u5b83\u5011\u662f\u5426\u6307\u5411\u76f8\u540c\u7684\u5b57\u4e32\u5c0d\u8c61\u3002\u9019\u6a23\u7684\u6bd4\u8f03\u901a\u5e38\u6bd4\u76f4\u63a5\u4f7f\u7528 `==` \u904b\u7b97\u7b26\u4f86\u5f97\u66f4\u5feb\uff0c\u56e0\u70ba\u5b83\u53ea\u9700\u8981\u6aa2\u67e5\u7269\u4ef6\u7684\u8eab\u4efd\u3002<\/p>\n<p>### \u6e1b\u5c11\u8a18\u61b6\u9ad4\u4f7f\u7528\u91cf<\/p>\n<p>\u4f7f\u7528 `intern()` \u51fd\u6578\u9084\u6709\u4e00\u500b\u91cd\u8981\u7684\u597d\u8655\u662f\u80fd\u5920\u6e1b\u5c11\u5b57\u4e32\u7684\u8a18\u61b6\u9ad4\u4f7f\u7528\u91cf\u3002\u5982\u679c\u5b57\u4e32\u6c60\u4e2d\u5df2\u7d93\u5b58\u5728\u67d0\u500b\u5b57\u4e32\uff0c\u5247\u4e0d\u9700\u8981\u5275\u5efa\u65b0\u7684\u5b57\u4e32\u5c0d\u8c61\uff0c\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u964d\u4f4e\u7a0b\u5e8f\u7684\u8a18\u61b6\u9ad4\u6d88\u8017\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `intern()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u554f\u984c\uff1a<\/p>\n<p>1. **\u5b57\u4e32\u672a\u88ab\u6b63\u78ba\u5b58\u5165\u5b57\u4e32\u6c60**\uff1a<br \/>\n   &#8211; \u78ba\u4fdd\u4f7f\u7528 `intern()` \u51fd\u6578\u4f86\u5b58\u5165\u5b57\u4e32\uff0c\u4e26\u6aa2\u67e5\u5b57\u4e32\u662f\u5426\u6b63\u78ba\u3002<\/p>\n<p>2. **\u6bd4\u8f03\u7d50\u679c\u4e0d\u6b63\u78ba**\uff1a<br \/>\n   &#8211; \u78ba\u8a8d\u4f7f\u7528 `is` \u4f86\u6bd4\u8f03\u5b57\u4e32\u7684\u8eab\u4efd\uff0c\u800c\u4e0d\u662f\u4f7f\u7528 `==` \u4f86\u6bd4\u8f03\u5167\u5bb9\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`intern()` \u51fd\u6578\u7684\u61c9\u7528\u4e0d\u50c5\u9650\u65bc\u5b57\u4e32\u6bd4\u8f03\uff0c\u9084\u53ef\u4ee5\u5728\u8655\u7406\u5927\u91cf\u91cd\u8907\u5b57\u4e32\u7684\u60c5\u6cc1\u4e0b\u986f\u8457\u63d0\u9ad8\u6548\u80fd\u3002\u5c0d\u65bc\u5927\u578b\u6578\u64da\u96c6\u6216\u9700\u8981\u983b\u7e41\u6bd4\u8f03\u5b57\u4e32\u7684\u61c9\u7528\uff0c\u4f7f\u7528 `intern()` \u662f\u4e00\u7a2e\u6709\u6548\u7684\u512a\u5316\u7b56\u7565\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cPython \u7684 `intern()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u6211\u5011\u5728\u5b57\u4e32\u8655\u7406\u4e0a\u63d0\u9ad8\u6548\u80fd\u4e26\u964d\u4f4e\u8a18\u61b6\u9ad4\u4f7f\u7528\u3002\u5c0d\u65bc\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u4e86\u89e3\u4e26\u6709\u6548\u4f7f\u7528\u9019\u500b\u51fd\u6578\u662f\u63d0\u9ad8 Python \u7a0b\u5f0f\u6027\u80fd\u7684\u95dc\u9375\u4e4b\u4e00\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u5176\u4ed6\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78](https:\/\/vocus.cc\/article\/60b2c123fd89780001cdb0c4)\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `intern()` \u51fd\u6578\u5728\u4ec0\u9ebc\u60c5\u6cc1\u4e0b\u6700\u6709\u7528\uff1f**<br \/>\nA1: `intern()` \u51fd\u6578\u5728\u9700\u8981\u983b\u7e41\u6bd4\u8f03\u5927\u91cf\u76f8\u540c\u5b57\u4e32\u7684\u60c5\u6cc1\u4e0b\u6700\u6709\u7528\uff0c\u7279\u5225\u662f\u5728\u5927\u578b\u6578\u64da\u96c6\u4e0a\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6548\u80fd\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u78ba\u5b9a\u5b57\u4e32\u662f\u5426\u5df2\u7d93\u88ab\u5167\u90e8\u5316\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `is` \u904b\u7b97\u7b26\u4f86\u6bd4\u8f03\u5b57\u4e32\u7684\u8eab\u4efd\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u8a8d\u5b57\u4e32\u662f\u5426\u6307\u5411\u540c\u4e00\u500b\u5c0d\u8c61\u3002<\/p>\n<p>**Q3: \u5728\u4f7f\u7528 `intern()` \u51fd\u6578\u6642\u9700\u8981\u6ce8\u610f\u4ec0\u9ebc\uff1f**<br \/>\nA3: \u78ba\u4fdd\u5728\u4f7f\u7528 `intern()` \u51fd\u6578\u4e4b\u524d\u5df2\u7d93\u5c0e\u5165 `sys` \u6a21\u7d44\uff0c\u4e26\u6ce8\u610f\u4e0d\u8981\u5c07\u4e0d\u53ef\u8b8a\u5b57\u4e32\uff08\u5982\u6578\u5b57\uff09\u50b3\u5165 `intern()`\uff0c\u56e0\u70ba\u9019\u6a23\u6703\u5c0e\u81f4\u932f\u8aa4\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7684Intern()\u51fd\u6578\u53ef\u4ee5\u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u70ba\u4e0d\u53ef\u8b8a\u7684\u5c0d\u8c61\uff0c\u4e26\u5c07\u5176\u5b58\u5132\u5728\u5167\u90e8\u5b57\u5178\u4e2d\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Intern()\u51fd\u6578\u7684\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\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-3382","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-Sy","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3382","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=3382"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3382\/revisions"}],"predecessor-version":[{"id":12849,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3382\/revisions\/12849"}],"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=3382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}