{"id":205047,"date":"2025-05-29T12:18:16","date_gmt":"2025-05-29T04:18:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205047\/"},"modified":"2025-05-29T12:18:16","modified_gmt":"2025-05-29T04:18:16","slug":"python-3-12%e4%b8%ad__int__%e5%86%99%e9%94%99%e5%af%bc%e8%87%b4%e6%8a%a5%e9%94%99%ef%bc%9a%e5%a6%82%e4%bd%95%e6%ad%a3%e7%a1%ae%e5%88%9d%e5%a7%8b%e5%8c%96%e7%b1%bb%e5%b1%9e%e6%80%a7%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205047\/","title":{"rendered":"Python 3.12\u4e2d__int__\u5199\u9519\u5bfc\u81f4\u62a5\u9519\uff1a\u5982\u4f55\u6b63\u786e\u521d\u59cb\u5316\u7c7b\u5c5e\u6027\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python 3.12\u4e2d__int__\u5199\u9519\u5bfc\u81f4\u62a5\u9519\uff1a\u5982\u4f55\u6b63\u786e\u521d\u59cb\u5316\u7c7b\u5c5e\u6027\uff1f<\/h1>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241122\/173225092867400d30c9256.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python 3.12 \u4e2d\u4f7f\u7528 class <\/strong>__init__* <strong>\u5c5e\u6027\u62a5\u9519<\/strong><\/p>\n<p>\u5728 python 3.12 \u4e2d\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u4f60\u9047\u5230\u4e86\u4e00\u4e2a\u9519\u8bef\uff0c\u63d0\u793a\u627e\u4e0d\u5230\u4e00\u4e2a\u7c7b\u5c5e\u6027\u3002\u9519\u8bef\u4fe1\u606f\u6307\u51fa\uff0c\u5bf9\u8c61\u6ca1\u6709\u540d\u4e3a <em>conf<\/em> \u6216 <em>name<\/em> \u7684\u5c5e\u6027\u3002<\/p>\n<p>\u4ed4\u7ec6\u68c0\u67e5\u4f60\u7684\u4ee3\u7801\u540e\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u95ee\u9898\u51fa\u5728 <em>class getconfig<\/em> \u7684\u6784\u9020\u51fd\u6570\u540d\u79f0\u4e0a\u3002\u4f60\u5199\u7684\u662f <em>__int__<\/em>\uff0c\u800c\u4e0d\u662f <em>__init__<\/em>\u3002\u5728 python \u4e2d\uff0c\u7279\u6b8a\u7684\u53cc\u4e0b\u5212\u7ebf\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u7279\u5b9a\u76ee\u7684\u3002<\/p>\n<p><em>__init__<\/em> \u662f\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u5b83\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u88ab\u8c03\u7528\u3002\u5b83\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u800c <em>__int__<\/em> \u662f\u7528\u6765\u5c06\u5bf9\u8c61\u8868\u793a\u4e3a\u6574\u6570\u65f6\u7684\u7279\u6b8a\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u9700\u8981\u5c06 <em>__int__<\/em> \u66f4\u6b63\u4e3a <em>__init__<\/em>\u3002\u4ee5\u4e0b\u662f\u66f4\u6b63\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>class GetConfig(object):\n    def __init__(self):  # \u6539\u4e3a __init__\n        current_dir = os.path.dirname(os.path.abspath(__file__))\n        print(current_dir)\n        sys_cfg_file = os.path.join(current_dir, \"sysConfig.cfg\")\n        self.conf = configparser.ConfigParser()\n        self.conf.read(sys_cfg_file)\n\n    def get_db_host(self):\n        db_host = self.conf.get(\"DB\", \"host\")\n        return db_host\n\nif __name__ == \"__main__\":\n    gc1 = GetConfig()\n    var = gc1.get_db_host()<\/pre>\n<p>\u66f4\u6b63\u4e4b\u540e\uff0c\u4ee3\u7801\u5e94\u8be5\u53ef\u4ee5\u6b63\u786e\u8fd0\u884c\uff0c\u5e76\u4e14\u80fd\u591f\u8bbf\u95ee\u7c7b\u5c5e\u6027 <em>conf<\/em> \u548c <em>name<\/em>\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPython 3.12\u4e2d__int__\u5199\u9519\u5bfc\u81f4\u62a5\u9519\uff1a\u5982\u4f55\u6b63\u786e\u521d\u59cb\u5316\u7c7b\u5c5e\u6027\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>Python 3.12\u4e2d__in&#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-205047","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205047","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=205047"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205047\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}