{"id":9264,"date":"2024-04-23T18:57:23","date_gmt":"2024-04-23T10:57:23","guid":{"rendered":"\/?p=9264"},"modified":"2024-04-23T18:57:42","modified_gmt":"2024-04-23T10:57:42","slug":"%e8%ae%a9%e7%b1%bb%e5%83%8f%e5%ad%97%e5%85%b8%e4%b8%80%e6%a0%b7%e5%b7%a5%e4%bd%9c","status":"publish","type":"post","link":"\/?p=9264","title":{"rendered":"\u8ba9\u7c7b\u50cf\u5b57\u5178\u4e00\u6837\u5de5\u4f5c"},"content":{"rendered":"<p>SettingsManager \u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a attributes \u5b57\u5178\u6765\u5b58\u50a8\u8bbe\u7f6e\uff0c\u4f46\u662f <strong>getitem<\/strong>\u3001<strong>setitem<\/strong> \u548c <strong>delitem<\/strong> \u65b9\u6cd5\u90fd\u662f\u7a7a\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u6ca1\u6709\u5b9e\u73b0\u4efb\u4f55\u529f\u80fd\u3002\u4e3a\u4e86\u8ba9\u8fd9\u4e2a\u7c7b\u80fd\u591f\u50cf\u5b57\u5178\u4e00\u6837\u5de5\u4f5c\uff08\u5373\u53ef\u4ee5\u4f7f\u7528 settings_manager['key'] \u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u548c\u8bbe\u7f6e\u503c\uff09\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u8fd9\u4e9b\u7279\u6b8a\u65b9\u6cd5\u63d0\u4f9b\u5b9e\u73b0\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u8fd9\u4e9b\u65b9\u6cd5\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u5b83\u4eec\u5141\u8bb8\u60a8\u901a\u8fc7\u952e\u6765\u83b7\u53d6\u3001\u8bbe\u7f6e\u548c\u5220\u9664 attributes \u5b57\u5178\u4e2d\u7684\u503c\uff1a<\/p>\n<pre><code class=\"language-python\">#!\/usr\/bin\/env python  \n# -*- coding: utf-8 -*-  \n&quot;&quot;&quot;  \n@Time    : 2024-04-23 18:48  \n@Author  : lxc  \n@File    : setting_manager.py  \n@Desc    :  \n&quot;&quot;&quot;  \n\nclass SettingsManager:  \n\n    def __init__(self):  \n        self.attributes = {  \n            &#039;PROJECT_NAME&#039;: &quot;baidu_spider&quot;,  \n            &#039;CONCURRENCY&#039;: 16  \n        }  \n\n    def __getitem__(self, item):  \n        &quot;&quot;&quot;\u901a\u8fc7\u952e\u83b7\u53d6\u503c&quot;&quot;&quot;  \n        return self.attributes[item]  \n\n    def __setitem__(self, key, value):  \n        &quot;&quot;&quot;\u901a\u8fc7\u952e\u8bbe\u7f6e\u503c&quot;&quot;&quot;  \n        self.attributes[key] = value  \n\n    def __delitem__(self, key):  \n        &quot;&quot;&quot;\u901a\u8fc7\u952e\u5220\u9664\u503c&quot;&quot;&quot;  \n        del self.attributes[key]  \n\n# \u4f7f\u7528\u793a\u4f8b  \nsettings_manager = SettingsManager()  \n\n# \u83b7\u53d6\u503c  \nprint(settings_manager[&#039;PROJECT_NAME&#039;])  # \u8f93\u51fa: baidu_spider  \n\n# \u8bbe\u7f6e\u503c  \nsettings_manager[&#039;CONCURRENCY&#039;] = 32  \nprint(settings_manager[&#039;CONCURRENCY&#039;])  # \u8f93\u51fa: 32  \n\n# \u5220\u9664\u503c  \ndel settings_manager[&#039;CONCURRENCY&#039;]  \n# print(settings_manager[&#039;CONCURRENCY&#039;])  # \u8fd9\u4f1a\u629b\u51fa KeyError\uff0c\u56e0\u4e3a CONCURRENCY \u5df2\u7ecf\u88ab\u5220\u9664<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u5b9e\u73b0\u4e2d\uff1a<\/p>\n<p><code>__getitem__<\/code> \u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u952e\uff08item\uff09\uff0c\u5e76\u8fd4\u56de attributes \u5b57\u5178\u4e2d\u5bf9\u5e94\u7684\u503c\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5b83\u5c06\u5f15\u53d1 KeyError\u3002<br \/>\n<code>__setitem__<\/code> \u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u952e\uff08key\uff09\u548c\u4e00\u4e2a\u503c\uff08value\uff09\uff0c\u5e76\u5c06\u8be5\u952e\u503c\u5bf9\u6dfb\u52a0\u5230 attributes \u5b57\u5178\u4e2d\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u66f4\u65b0\u5176\u5bf9\u5e94\u7684\u503c\u3002<br \/>\n<code>__delitem__<\/code> \u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u952e\uff08key\uff09\uff0c\u5e76\u4ece attributes \u5b57\u5178\u4e2d\u5220\u9664\u5bf9\u5e94\u7684\u952e\u503c\u5bf9\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5b83\u5c06\u5f15\u53d1 KeyError\u3002<br \/>\n\u73b0\u5728\uff0cSettingsManager \u7c7b\u5c31\u53ef\u4ee5\u50cf\u4f7f\u7528\u5b57\u5178\u4e00\u6837\u6765\u64cd\u4f5c\u5176\u5185\u90e8\u7684 attributes \u5b57\u5178\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SettingsManager \u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a attributes \u5b57\u5178\u6765\u5b58\u50a8\u8bbe\u7f6e\uff0c\u4f46\u662f getitem\u3001setitem \u548c delit   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[21],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9264"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9264"}],"version-history":[{"count":2,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9264\/revisions"}],"predecessor-version":[{"id":9266,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9264\/revisions\/9266"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9264"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}