{"id":204746,"date":"2025-05-29T13:57:33","date_gmt":"2025-05-29T05:57:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204746\/"},"modified":"2025-05-29T13:57:33","modified_gmt":"2025-05-29T05:57:33","slug":"python-%e7%b1%bb%e6%96%b9%e6%b3%95%e7%9a%84%e7%89%b9%e6%ae%8a%e5%a4%84%e7%90%86%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e8%b0%83%e7%94%a8-__getattr__-%e5%8d%b4%e7%9b%b4%e6%8e%a5%e6%89%a7%e8%a1%8c%e6%96%b9","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204746\/","title":{"rendered":"Python \u7c7b\u65b9\u6cd5\u7684\u7279\u6b8a\u5904\u7406\uff1a\u4e3a\u4ec0\u4e48\u8c03\u7528 __getattr__ \u5374\u76f4\u63a5\u6267\u884c\u65b9\u6cd5\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u7c7b\u65b9\u6cd5\u7684\u7279\u6b8a\u5904\u7406\uff1a\u4e3a\u4ec0\u4e48\u8c03\u7528 __getattr__ \u5374\u76f4\u63a5\u6267\u884c\u65b9\u6cd5\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241115\/17316389296736b691e1439.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u7c7b\u65b9\u6cd5\u7684\u7279\u6b8a\u5904\u7406<\/strong><\/p>\n<p>\u5728 python \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 __getattr__ \u65b9\u6cd5\u62e6\u622a\u7c7b\u7684\u5c5e\u6027\u83b7\u53d6\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u5f53\u8be5\u5c5e\u6027\u5b58\u5728\u4e8e\u7c7b\u7684\u57fa\u7c7b\u6216\u5b50\u7c7b\u4e2d\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e0d\u7b26\u5408\u9884\u671f\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u95ee\u9898\uff1a\u8c03\u7528 __getattr__ \u5374\u76f4\u63a5\u6267\u884c\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728\u6240\u63d0\u4f9b\u7684\u793a\u4f8b\u4e2d\uff0cpointer \u7c7b\u5b9a\u4e49\u4e86 __getattr__ \u65b9\u6cd5\uff0c\u7528\u4e8e\u62e6\u622a\u5bf9 __value \u5c5e\u6027\u7684\u8bbf\u95ee\u3002\u7136\u800c\uff0c\u5728\u8c03\u7528 s.__str__ \u65f6\uff0c\u5b83\u5374\u76f4\u63a5\u6267\u884c\u4e86 str \u7c7b\u7684 __str__ \u65b9\u6cd5\uff0c\u800c\u6ca1\u6709\u7ecf\u8fc7 pointer \u7c7b\u7684 __getattr__ \u65b9\u6cd5\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a\u5b9a\u4e49\u4e00\u4e2a __str__ \u65b9\u6cd5<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u5728 pointer \u7c7b\u4e2d\u76f4\u63a5\u5b9a\u4e49\u4e00\u4e2a __str__ \u65b9\u6cd5\u3002\u5f53\u8c03\u7528 s.__str__ \u65f6\uff0cpython \u5c06\u76f4\u63a5\u4f7f\u7528 pointer \u7c7b\u4e2d\u7684 __str__ \u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u901a\u8fc7 str(s) \u8f6c\u6362\u540e\u518d\u8c03\u7528 str \u7c7b\u7684 __str__ \u65b9\u6cd5\u3002<\/p>\n<p>\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>class Pointer:\n    ...\n\n    def __str__(self):\n        return str(self.__value)<\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cs.__str__ \u5c06\u8c03\u7528 pointer \u7c7b\u4e2d\u7684 __str__ \u65b9\u6cd5\uff0c\u4ece\u800c\u4f7f\u7528 pointer \u7c7b\u7684 __getattr__ \u65b9\u6cd5\u62e6\u622a\u5bf9 __value \u5c5e\u6027\u7684\u8bbf\u95ee\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPython \u7c7b\u65b9\u6cd5\u7684\u7279\u6b8a\u5904\u7406\uff1a\u4e3a\u4ec0\u4e48\u8c03\u7528 __getattr__ \u5374\u76f4\u63a5\u6267\u884c\u65b9\u6cd5\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u7c7b\u65b9\u6cd5\u7684\u7279\u6b8a\u5904\u7406\uff1a&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204746","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204746"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204746\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}