{"id":3548,"date":"2025-06-01T14:51:33","date_gmt":"2025-06-04T07:21:33","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3548"},"modified":"2025-06-04T14:51:33","modified_gmt":"2025-06-04T07:21:33","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e5%bc%b1%e5%8f%83%e7%85%a7weakref%e7%9a%84%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e5%bc%b1%e5%8f%83%e7%85%a7weakref%e7%9a%84%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 weakref()\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, weakref, \u5f31\u53c3\u7167, Python \u6559\u5b78, Python \u5be6\u4f5c, Python \u932f\u8aa4\u6392\u9664\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 weakref()<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`weakref` \u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u8b93\u958b\u767c\u8005\u5275\u5efa\u5f31\u53c3\u7167\uff08weak reference\uff09\uff0c\u9019\u610f\u5473\u8457\u4f60\u53ef\u4ee5\u53c3\u8003\u4e00\u500b\u7269\u4ef6\u800c\u4e0d\u589e\u52a0\u8a72\u7269\u4ef6\u7684\u5f15\u7528\u8a08\u6578\u3002\u9019\u5c0d\u65bc\u907f\u514d\u5faa\u74b0\u53c3\u7167\u5c0e\u81f4\u7684\u8a18\u61b6\u9ad4\u6d29\u6f0f\u975e\u5e38\u6709\u7528\u3002\u672c\u6587\u5c07\u4ecb\u7d39 `weakref` \u7684\u57fa\u672c\u7528\u6cd5\u3001\u61c9\u7528\u7bc4\u4f8b\u53ca\u932f\u8aa4\u6392\u9664\u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5ef6\u4f38\u61c9\u7528\u7684\u5efa\u8b70\u3002<\/p>\n<p><!--more--><\/p>\n<h2>weakref() \u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>\u4f7f\u7528 `weakref` \u7684\u57fa\u672c\u65b9\u6cd5\u662f\u5c07\u4e00\u500b\u7269\u4ef6\u4f5c\u70ba\u53c3\u6578\u50b3\u5165 `weakref.ref()` \u51fd\u5f0f\uff0c\u4e26\u5c07\u56de\u50b3\u503c\u5b58\u5165\u4e00\u500b\u8b8a\u6578\u4e2d\u3002\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u793a\u7bc4\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport weakref<\/p>\n<p>class SomeObject:<br \/>\n    def __init__(self, name):<br \/>\n        self.name = name<\/p>\n<p># \u5275\u5efa\u4e00\u500b\u7269\u4ef6<br \/>\nmy_obj = SomeObject(&#8220;\u793a\u4f8b\u7269\u4ef6&#8221;)<\/p>\n<p># \u5275\u5efa\u5f31\u53c3\u7167<br \/>\nweak_ref = weakref.ref(my_obj)<\/p>\n<p># \u53d6\u5f97\u7269\u4ef6\u8cc7\u8a0a<br \/>\nprint(weak_ref())  # \u8f38\u51fa\uff1a<__main__.SomeObject object at 0x...><\/p>\n<p># \u522a\u9664\u539f\u7269\u4ef6<br \/>\ndel my_obj<\/p>\n<p># \u5617\u8a66\u53d6\u5f97\u7269\u4ef6\u8cc7\u8a0a<br \/>\nprint(weak_ref())  # \u8f38\u51fa\uff1aNone\uff0c\u56e0\u70ba\u539f\u7269\u4ef6\u5df2\u88ab\u522a\u9664<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b `SomeObject` \u985e\u5225\u5be6\u4f8b\uff0c\u4e26\u4f7f\u7528 `weakref.ref()` \u5275\u5efa\u4e86\u4e00\u500b\u5c0d\u8a72\u7269\u4ef6\u7684\u5f31\u53c3\u7167\u3002\u7576\u539f\u7269\u4ef6\u88ab\u522a\u9664\u5f8c\uff0c\u5f31\u53c3\u7167\u5c07\u8fd4\u56de `None`\uff0c\u9019\u8868\u660e\u4e0d\u518d\u6709\u5c0d\u8a72\u7269\u4ef6\u7684\u5f37\u5f15\u7528\u3002<\/p>\n<h2>weakref() \u7684\u61c9\u7528<\/h2>\n<p>`weakref` \u7684\u61c9\u7528\u5834\u666f\u975e\u5e38\u5ee3\u6cdb\uff0c\u4ee5\u4e0b\u662f\u5e7e\u500b\u5be6\u969b\u7684\u4f7f\u7528\u6848\u4f8b\uff1a<\/p>\n<p>1. **\u89e3\u6c7a\u5faa\u74b0\u53c3\u7167**\uff1a\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u5169\u500b\u7269\u4ef6\u4e92\u76f8\u5f15\u7528\u53ef\u80fd\u5c0e\u81f4\u8a18\u61b6\u9ad4\u7121\u6cd5\u91cb\u653e\uff0c\u4f7f\u7528 `weakref` \u53ef\u4ee5\u6253\u7834\u9019\u7a2e\u5faa\u74b0\u3002<\/p>\n<p>2. **\u7269\u4ef6\u5feb\u53d6**\uff1a\u53ef\u4ee5\u5229\u7528\u5f31\u53c3\u7167\u4f86\u5be6\u73fe\u7269\u4ef6\u5feb\u53d6\uff0c\u7576\u5feb\u53d6\u4e2d\u7684\u7269\u4ef6\u4e0d\u518d\u88ab\u4f7f\u7528\u6642\uff0c\u81ea\u52d5\u91cb\u653e\u8a18\u61b6\u9ad4\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 `weakref` \u5be6\u73fe\u5feb\u53d6\u7684\u7c21\u55ae\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Cache:<br \/>\n    def __init__(self):<br \/>\n        self._cache = weakref.WeakValueDictionary()<\/p>\n<p>    def get(self, key):<br \/>\n        return self._cache.get(key)<\/p>\n<p>    def set(self, key, value):<br \/>\n        self._cache[key] = value<\/p>\n<p># \u4f7f\u7528\u5feb\u53d6<br \/>\ncache = Cache()<br \/>\ncache.set(&#8220;example&#8221;, SomeObject(&#8220;\u5feb\u53d6\u7269\u4ef6&#8221;))<br \/>\nprint(cache.get(&#8220;example&#8221;))  # \u8f38\u51fa\u7269\u4ef6\u8cc7\u8a0a<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `weakref` \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u554f\u984c\uff1a<\/p>\n<p>&#8211; **\u8fd4\u56de None**\uff1a\u5982\u679c\u5f31\u53c3\u7167\u8fd4\u56de `None`\uff0c\u8acb\u6aa2\u67e5\u539f\u7269\u4ef6\u662f\u5426\u5df2\u7d93\u88ab\u522a\u9664\u3002<br \/>\n&#8211; **\u7121\u6cd5\u4f7f\u7528 `weakref`**\uff1a\u78ba\u4fdd\u4f60\u6240\u5f15\u7528\u7684\u7269\u4ef6\u662f\u53ef\u54c8\u5e0c\u7684\uff0c\u5426\u5247\u5c07\u7121\u6cd5\u4f7f\u7528 `weakref`\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u4e0a\u8ff0\u61c9\u7528\uff0c`weakref` \u9084\u53ef\u4ee5\u7528\u65bc\u4ee5\u4e0b\u60c5\u5883\uff1a<\/p>\n<p>&#8211; **\u4e8b\u4ef6\u7cfb\u7d71**\uff1a\u5728\u4e8b\u4ef6\u9a45\u52d5\u7684\u8a2d\u8a08\u4e2d\uff0c\u4f7f\u7528\u5f31\u53c3\u7167\u4f86\u8a3b\u518a\u4e8b\u4ef6\u76e3\u807d\u5668\uff0c\u907f\u514d\u5167\u5b58\u6d29\u6f0f\u3002<br \/>\n&#8211; **\u8cc7\u6e90\u7ba1\u7406**\uff1a\u5728\u9700\u8981\u7ba1\u7406\u5927\u91cf\u8cc7\u6e90\u7684\u61c9\u7528\u4e2d\uff0c\u4f7f\u7528\u5f31\u53c3\u7167\u8ffd\u8e64\u8cc7\u6e90\u7684\u4f7f\u7528\u60c5\u6cc1\u3002<\/p>\n<p>\u4f7f\u7528 `weakref` \u80fd\u5920\u4f7f\u4f60\u7684 Python \u7a0b\u5f0f\u66f4\u52a0\u9ad8\u6548\u548c\u5065\u58ef\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u4e2d\u7684 `weakref()` \u51fd\u5f0f\u63d0\u4f9b\u4e86\u4e00\u7a2e\u5275\u5efa\u5f31\u53c3\u7167\u7684\u65b9\u6cd5\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u89e3\u6c7a\u5faa\u74b0\u53c3\u7167\u554f\u984c\u4e26\u63d0\u5347\u7a0b\u5f0f\u6027\u80fd\u3002\u900f\u904e\u672c\u6587\u7684\u4ecb\u7d39\uff0c\u5e0c\u671b\u4f60\u80fd\u5920\u638c\u63e1 `weakref` \u7684\u57fa\u672c\u7528\u6cd5\u53ca\u5176\u61c9\u7528\u5834\u666f\u3002<\/p>\n<p>\u5982\u9700\u66f4\u6df1\u5165\u7684 Python \u6559\u5b78\uff0c\u8acb\u53c3\u8003 [vocus.cc \u7684 Python \u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u4ec0\u9ebc\u662f\u5f31\u53c3\u7167\uff0c\u8207\u5f37\u53c3\u7167\u6709\u4f55\u4e0d\u540c\uff1f<\/h3>\n<p>\u5f31\u53c3\u7167\u4e0d\u6703\u589e\u52a0\u7269\u4ef6\u7684\u5f15\u7528\u8a08\u6578\uff0c\u7576\u7269\u4ef6\u53ea\u6709\u5f31\u53c3\u7167\u6642\uff0cPython \u5783\u573e\u56de\u6536\u5668\u6703\u81ea\u52d5\u91cb\u653e\u8a72\u7269\u4ef6\u7684\u8a18\u61b6\u9ad4\u3002\u5f37\u53c3\u7167\u5247\u6703\u4f7f\u7269\u4ef6\u5728\u5f15\u7528\u5b58\u5728\u6642\u4e0d\u6703\u88ab\u91cb\u653e\u3002<\/p>\n<h3>2. \u4f7f\u7528 weakref \u6642\u9700\u8981\u6ce8\u610f\u4ec0\u9ebc\uff1f<\/h3>\n<p>\u4f7f\u7528 `weakref` \u6642\uff0c\u9700\u6ce8\u610f\u7269\u4ef6\u7684\u751f\u547d\u9031\u671f\uff0c\u78ba\u4fdd\u4e0d\u6703\u5728\u7269\u4ef6\u88ab\u91cb\u653e\u5f8c\u5617\u8a66\u5b58\u53d6\u5176\u5f31\u53c3\u7167\u3002<\/p>\n<h3>3. weakref \u53ef\u4ee5\u7528\u4f86\u505a\u4ec0\u9ebc\uff1f<\/h3>\n<p>`weakref` \u53ef\u4ee5\u7528\u4f86\u89e3\u6c7a\u5faa\u74b0\u53c3\u7167\u554f\u984c\u3001\u5be6\u73fe\u7269\u4ef6\u5feb\u53d6\u3001\u8a3b\u518a\u4e8b\u4ef6\u76e3\u807d\u5668\u7b49\u591a\u7a2e\u7528\u9014\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u4e86\u89e3Python\u4e2d\u5f31\u53c3\u7167(Weakref)\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u5728\u4e0d\u5f71\u97ff\u539f\u6709\u7269\u4ef6\u7684\u60c5\u6cc1\u4e0b\uff0c\u5efa\u7acb\u4e00\u500b\u53c3\u7167\u7269\u4ef6\uff0c\u4e26\u53ef\u4ee5\u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u4ecd\u5b58\u5728\u3002\u672c\u6587\u5c07\u4ecb\u7d39Python\u4e2d\u5f31\u53c3\u7167(Weakref)\u7684\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u6539\u5584\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\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-3548","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-Ve","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3548","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=3548"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3548\/revisions"}],"predecessor-version":[{"id":3549,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3548\/revisions\/3549"}],"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=3548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}