{"id":204726,"date":"2025-05-29T16:00:43","date_gmt":"2025-05-29T08:00:43","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204726\/"},"modified":"2025-05-29T16:00:43","modified_gmt":"2025-05-29T08:00:43","slug":"%e5%a6%82%e4%bd%95%e9%ab%98%e6%95%88%e5%9c%b0%e8%8e%b7%e5%8f%96%e5%92%8c%e8%ae%be%e7%bd%ae%e6%b7%b1%e5%ba%a6%e5%b5%8c%e5%a5%97%e7%9a%84%e5%ad%97%e5%85%b8%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204726\/","title":{"rendered":"\u5982\u4f55\u9ad8\u6548\u5730\u83b7\u53d6\u548c\u8bbe\u7f6e\u6df1\u5ea6\u5d4c\u5957\u7684\u5b57\u5178\u503c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u9ad8\u6548\u5730\u83b7\u53d6\u548c\u8bbe\u7f6e\u6df1\u5ea6\u5d4c\u5957\u7684\u5b57\u5178\u503c\uff1f<\/h1>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5982\u4f55\u9ad8\u6548\u5730\u83b7\u53d6\u548c\u8bbe\u7f6e\u6df1\u5ea6\u5d4c\u5957\u7684\u5b57\u5178\u503c\uff1f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241114\/17315807696735d361abd3a.jpg\" class=\"aligncenter\"><\/p>\n<p>\u60a8\u6b63\u5728\u5bfb\u627e\u4e00\u79cd\u8f7b\u677e\u64cd\u4f5c\u6df1\u5ea6\u5d4c\u5957\u5b57\u5178\u7684\u65b9\u6cd5\uff0c\u65e0\u9700\u5927\u91cf\u7684\u4ee3\u7801\u3002\u5728\u672c\u6307\u5357\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5b9e\u73b0\u6b64\u76ee\u6807\u7684\u4e0d\u540c\u7b56\u7565\uff1a<\/p>\n<ol>\n<li>\u5bfc\u5165 dotdictify \u5e93\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a dotdictify \u5bf9\u8c61\u3002<\/li>\n<li>\u4f7f\u7528\u70b9\u8bed\u6cd5\u83b7\u53d6\u548c\u8bbe\u7f6e\u503c\u3002<\/li>\n<\/ol>\n<pre>from dotdictify import dotdictify\n\nmy_obj = dotdictify()\nmy_obj.a.b.c = {'d': 1, 'e': 2}\nprint(my_obj.a.b.c.d)  # \u8f93\u51fa\uff1a1<\/pre>\n<p>\u5982\u679c\u60a8\u66f4\u559c\u6b22\u6269\u5c55\u5185\u7f6e dict \u7c7b\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8986\u76d6 <strong>getitem<\/strong> \u548c <strong>setitem<\/strong> \u65b9\u6cd5\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u884c\u4e3a\u3002<\/p>\n<pre>class dotteddict(dict):\n    def __getitem__(self, key):\n        if '.' in key:\n            mykey, restofkey = key.split('.', 1)\n            return self[mykey][restofkey]\n        return dict.__getitem__(self, key)\n\n    def __setitem__(self, key, value):\n        if '.' in key:\n            mykey, restofkey = key.split('.', 1)\n            self.setdefault(mykey, {}).__setitem__(restofkey, value)\n        else:\n            dict.__setitem__(self, key, value)<\/pre>\n<p>\u4f7f\u7528\u6b64\u6269\u5c55\uff1a<\/p>\n<pre>my_obj = dotteddict()\nmy_obj['a.b.c'] = {'d': 1, 'e': 2}\nprint(my_obj['a.b.c']['d'])  # \u8f93\u51fa\uff1a1<\/pre>\n<p>dict \u7c7b\u8fd8\u63d0\u4f9b\u4e86\u66ff\u4ee3\u65b9\u6848\uff0c\u4f8b\u5982 setdefault \u548c getdefault \u65b9\u6cd5\u3002<\/p>\n<pre>my_obj = {}\nmy_obj.setdefault('a.b.c', {}).setdefault('d', 1)\nprint(my_obj.getdefault('a.b.c.d', None))  # \u8f93\u51fa\uff1a1<\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u548c\u8bbe\u7f6e\u5d4c\u5957\u7684\u5b57\u5178\u503c\uff0c\u5e76\u6307\u5b9a\u9ed8\u8ba4\u503c\u4ee5\u5904\u7406\u4e0d\u5b58\u5728\u7684\u952e\u3002<\/p>\n<p>\u6839\u636e\u60a8\u7684\u5177\u4f53\u9700\u6c42\uff0c\u9009\u62e9\u9002\u5408\u60a8\u7684\u7b56\u7565\u3002\u6839\u636e\u60a8\u7684\u4e2a\u4eba\u559c\u597d\u548c\u7528\u4f8b\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u9009\u9879\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u9ad8\u6548\u5730\u83b7\u53d6\u548c\u8bbe\u7f6e\u6df1\u5ea6\u5d4c\u5957\u7684\u5b57\u5178\u503c\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u9ad8\u6548\u5730\u83b7\u53d6\u548c\u8bbe\u7f6e\u6df1\u5ea6\u5d4c\u5957\u7684\u5b57&#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-204726","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204726","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=204726"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204726\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}