{"id":204656,"date":"2025-05-29T12:11:45","date_gmt":"2025-05-29T04:11:45","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204656\/"},"modified":"2025-05-29T12:11:45","modified_gmt":"2025-05-29T04:11:45","slug":"python-%e7%b1%bb%e4%b8%ad%e7%9a%84%e9%93%be%e5%bc%8f%e8%b0%83%e7%94%a8%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e6%af%8f%e6%ac%a1%e8%be%93%e5%87%ba%e5%af%b9%e8%b1%a1%e7%9a%84-id-%e9%83%bd%e4%b8%8d%e5%90%8c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204656\/","title":{"rendered":"Python \u7c7b\u4e2d\u7684\u94fe\u5f0f\u8c03\u7528\uff1a\u4e3a\u4ec0\u4e48\u6bcf\u6b21\u8f93\u51fa\u5bf9\u8c61\u7684 id \u90fd\u4e0d\u540c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u7c7b\u4e2d\u7684\u94fe\u5f0f\u8c03\u7528\uff1a\u4e3a\u4ec0\u4e48\u6bcf\u6b21\u8f93\u51fa\u5bf9\u8c61\u7684 id \u90fd\u4e0d\u540c\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPython \u7c7b\u4e2d\u7684\u94fe\u5f0f\u8c03\u7528\uff1a\u4e3a\u4ec0\u4e48\u6bcf\u6b21\u8f93\u51fa\u5bf9\u8c61\u7684 id \u90fd\u4e0d\u540c\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241112\/173140708667332cee0c99a.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u7c7b\u4e2d\u7684\u5185\u5b58\u5206\u914d\u95ee\u9898<\/strong><\/p>\n<p>\u5728 python \u4e2d\u521b\u5efa\u7c7b\u65f6\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u7684\u5185\u5b58\u5206\u914d\u65b9\u5f0f\u53ef\u80fd\u4ee4\u521d\u5b66\u8005\u611f\u5230\u56f0\u60d1\u3002\u4f8b\u5982\uff0c\u5728\u94fe\u5f0f\u8c03\u7528\u7684\u60c5\u51b5\u4e0b\uff0c\u6bcf\u6b21\u8f93\u51fa\u5bf9\u8c61\u7684 id \u65f6\u90fd\u4f1a\u770b\u5230\u4e0d\u540c\u7684\u503c\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u4ece\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\u5f00\u59cb\uff1a<\/p>\n<pre># \u94fe\u5f0f\u8c03\u7528\nclass chain(object):\n    def __init__(self, path=''):\n        self._path = path\n\n    def __getattr__(self, path):\n        return chain('%s\/%s' % (self._path, path))\n\n    def __str__(self):\n        return self._path\n\n    __repr__ = __str__\n\nchain = chain()\nchain.wenzhou  # \u6bcf\u6b21\u8c03\u7528 id \u4e0d\u540c<\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6bcf\u6b21\u8c03\u7528 chain.wenzhou \u65f6\uff0c\u6211\u4eec\u4f1a\u770b\u5230\u4e0d\u540c\u7684 id\u3002\u8fd9\u662f\u56e0\u4e3a __getattr__ \u65b9\u6cd5\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u90fd\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 chain \u5b9e\u4f8b\u3002<\/p>\n<p>\u4e3a\u4e86\u7406\u89e3\u8fd9\u4e00\u70b9\uff0c\u8ba9\u6211\u4eec\u68c0\u67e5\u4e00\u4e0b __getattr__ \u65b9\u6cd5\uff1a<\/p>\n<pre>def __getattr__(self, path):\n    return chain('%s\/%s' % (self._path, path))<\/pre>\n<p>\u5728\u8be5\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5f53\u524d\u5bf9\u8c61\u7684\u8def\u5f84 (self._path) \u548c\u4f20\u9012\u7684\u8def\u5f84 (path) \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 chain \u5b9e\u4f8b\u3002<\/p>\n<p>\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4fee\u6539 __getattr__ \u65b9\u6cd5\u5e76\u8fd4\u56de\u5f53\u524d\u5bf9\u8c61\u672c\u8eab\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\uff1a<\/p>\n<pre>def __getattr__(self, path):\n    return self<\/pre>\n<p>\u901a\u8fc7\u6b64\u66f4\u6539\uff0c\u6bcf\u6b21\u8c03\u7528 chain.wenzhou \u65f6\uff0c\u6211\u4eec\u90fd\u4f1a\u83b7\u5f97\u540c\u4e00\u5bf9\u8c61\u7684 id\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u7c7b\u4e2d\u7684\u94fe\u5f0f\u8c03\u7528\uff1a\u4e3a&#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-204656","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204656","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=204656"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204656\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}