{"id":205037,"date":"2025-05-29T09:17:34","date_gmt":"2025-05-29T01:17:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205037\/"},"modified":"2025-05-29T09:17:34","modified_gmt":"2025-05-29T01:17:34","slug":"python-3-12%e4%b8%ad__int__%e5%af%bc%e8%87%b4%e5%b1%9e%e6%80%a7%e4%b8%8d%e5%8f%af%e7%94%a8%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e6%88%91%e7%9a%84getconfig%e5%af%b9%e8%b1%a1%e6%b2%a1%e6%9c%89conf","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205037\/","title":{"rendered":"Python 3.12\u4e2d`__int__`\u5bfc\u81f4\u5c5e\u6027\u4e0d\u53ef\u7528\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684GetConfig\u5bf9\u8c61\u6ca1\u6709\\&#8217;conf\\&#8217;\u5c5e\u6027\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python 3.12\u4e2d`__int__`\u5bfc\u81f4\u5c5e\u6027\u4e0d\u53ef\u7528\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684GetConfig\u5bf9\u8c61\u6ca1\u6709\\&#8217;conf\\&#8217;\u5c5e\u6027\uff1f<\/h1>\n<p>\u5b66\u4e60\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPython 3.12\u4e2d`__int__`\u5bfc\u81f4\u5c5e\u6027\u4e0d\u53ef\u7528\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684GetConfig\u5bf9\u8c61\u6ca1\u6709\\&#8217;conf\\&#8217;\u5c5e\u6027\uff1f\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241122\/1732241020673fe67cbfd0d.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python 3.12 \u4e2d <strong>init<\/strong> \u4e2d\u7684\u5c5e\u6027\u4e0d\u53ef\u7528<\/strong><\/p>\n<p>\u5728 python 3.12 \u4e2d\u7f16\u5199\u4e86\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u4f46\u662f\u8fd0\u884c\u65f6\u9047\u5230\u4e86\u4e00\u4e2a\u9519\u8bef\uff0c\u63d0\u793a\u201cattributeerror\uff1agetconfig \u5bf9\u8c61\u6ca1\u6709\u5c5e\u6027 conf\u201d\u3002<\/p>\n<p>\u9519\u8bef\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>class getconfig(object):\n    def __int__(self):\n        # \u521b\u5efa\u4e00\u4e2a\u5c5e\u6027\n        self.conf = configparser.configparser()\n\n    def get_db_host(self):\n        return self.conf.get(\"db\", \"host\")\n\nif __name__ == \"__main__\":\n    gc1 = getconfig()\n    var = gc1.get_db_host()<\/pre>\n<p>\u9519\u8bef\u6d88\u606f\uff1a<\/p>\n<pre>traceback (most recent call last):\n  file \"getconfig.py\", line 21, in &lt;module&gt;\n    var = gc1.get_db_host()\n          ^^^^^^^^^^^^^^^^^\n  file \"getconfig.py\", line 17, in get_db_host\n    return self.conf.get(\"db\", \"host\")\n              ^^^^^^^^^\nattributeerror: 'getconfig' object has no attribute 'conf'<\/pre>\n<p>\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u6b64\u9519\u8bef\uff1f<\/p>\n<p>\u9519\u8bef\u7684\u6839\u672c\u539f\u56e0\u5728\u4e8e\u7c7b\u6784\u9020\u65b9\u6cd5\u7684\u62fc\u5199\u3002\u5728 python \u4e2d\uff0c\u7c7b\u6784\u9020\u65b9\u6cd5\u7684\u540d\u79f0\u662f __init__\uff0c\u800c\u4e0d\u662f __int__\u3002<\/p>\n<p>\u4fee\u6b63\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>class GetConfig(object):\n    def __init__(self):\n        # \u521b\u5efa\u4e00\u4e2a\u5c5e\u6027\n        self.conf = configparser.ConfigParser()\n\n    def get_db_host(self):\n        return self.conf.get(\"DB\", \"host\")\n\nif __name__ == \"__main__\":\n    gc1 = GetConfig()\n    var = gc1.get_db_host()<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPython 3.12\u4e2d`__int__`\u5bfc\u81f4\u5c5e\u6027\u4e0d\u53ef\u7528\uff1a\u4e3a\u4ec0\u4e48\u6211\u7684GetConfig\u5bf9\u8c61\u6ca1\u6709\\&#8217;conf\\&#8217;\u5c5e\u6027\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python 3.12\u4e2d`__i&#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-205037","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205037","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=205037"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205037\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}