{"id":204392,"date":"2025-05-29T09:55:32","date_gmt":"2025-05-29T01:55:32","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204392\/"},"modified":"2025-05-29T09:55:32","modified_gmt":"2025-05-29T01:55:32","slug":"python-%e5%ae%9e%e4%be%8b%e5%8c%96%e5%af%b9%e8%b1%a1%e6%97%b6%e6%8a%a5%e9%94%99typeerror-init-takes-2-positional-arguments-but-3-were-given%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204392\/","title":{"rendered":"Python \u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6\u62a5\u9519\u201cTypeError: init() takes 2 positional arguments but 3 were given\u201d\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6\u62a5\u9519\u201cTypeError: init() takes 2 positional arguments but 3 were given\u201d\u5982\u4f55\u89e3\u51b3\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPython \u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6\u62a5\u9519\u201cTypeError: init() takes 2 positional arguments but 3 were given\u201d\u5982\u4f55\u89e3\u51b3\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\/20241106\/1730881666672b2882e4022.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Python \u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6\u62a5\u9519\u7684\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728\u5b9e\u4f8b\u5316\u5bf9\u8c61\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8fd9\u6837\u7684\u9519\u8bef\u63d0\u793a\uff1aTypeError: <strong>init<\/strong>() takes 2 positional arguments but 3 were given\u3002\u8fd9\u8868\u660e\u8be5\u9519\u8bef\u53d1\u751f\u5728\u7c7b\u7684\u521d\u59cb\u5316\u51fd\u6570 <strong>init<\/strong>() \u4e2d\u3002<\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u7684\u539f\u56e0\u901a\u5e38\u662f\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u65f6\uff0c\u5728\u5b50\u7c7b\u7684 <strong>init<\/strong>() \u51fd\u6570\u4e2d\u4f20\u9012\u4e86\u8fc7\u591a\u6216\u8fc7\u5c11\u7684\u53c2\u6570\u3002\u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u7236\u7c7b People \u4f7f\u7528 <strong>init<\/strong>() \u51fd\u6570\u521d\u59cb\u5316 height \u548c money \u5c5e\u6027\uff0c\u800c\u5b50\u7c7b Man \u4f7f\u7528 <strong>init<\/strong>() \u51fd\u6570\u521d\u59cb\u5316 age \u5c5e\u6027\u3002<\/p>\n<p>\u7136\u800c\uff0c\u5728\u5b9e\u4f8b\u5316 Man \u7c7b\u65f6\uff0c\u5374\u4f20\u9012\u4e86 age \u548c parent class \u7684 height \u548c money \u53c2\u6570\uff0c\u5bfc\u81f4 <strong>init<\/strong>() \u51fd\u6570\u63a5\u6536\u5230\u4e86 3 \u4e2a\u53c2\u6570\uff0c\u800c\u5b83\u5b9e\u9645\u4e0a\u53ea\u80fd\u63a5\u6536 2 \u4e2a\u53c2\u6570\uff08\u52a0\u4e0a hidden parameter self\uff09\u3002\u56e0\u6b64\uff0c\u5f15\u53d1\u4e86 TypeError \u9519\u8bef\u3002<\/p>\n<p>\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u6b63\u786e\u65b9\u6cd5\u662f\u4ec5\u4f20\u9012\u5b50\u7c7b\u7279\u6709\u7684\u53c2\u6570\uff0c\u5bf9\u4e8e\u7236\u7c7b\u7684\u5c5e\u6027\uff0c\u5728\u5b50\u7c7b\u7684 <strong>init<\/strong>() \u51fd\u6570\u4e2d\u4f7f\u7528 super().__init__(&#8230;) \u8bed\u6cd5\u8c03\u7528\u7236\u7c7b\u7684 <strong>init<\/strong>() \u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n<p>\u6709\u5173\u5b50\u7c7b\u8c03\u7528\u7236\u7c7b\u521d\u59cb\u5316\u65b9\u6cd5\u7684\u66f4\u8be6\u7ec6\u793a\u4f8b\uff0c\u8bf7\u53c2\u9605\u4ee5\u4e0b\u6587\u7ae0\uff1a<\/p>\n<ul>\n<li>[Python \u4e2d\u5b50\u7c7b\u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5](https:\/\/blog.csdn.net\/lovemysea\/article\/details\/78836927)<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPython \u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6\u62a5\u9519\u201cTypeError: init() takes 2 positional arguments but 3 were given\u201d\u5982\u4f55\u89e3\u51b3\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 \u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6\u62a5\u9519\u201c&#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-204392","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204392","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=204392"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204392\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}