{"id":959064,"date":"2024-12-27T03:37:26","date_gmt":"2024-12-26T19:37:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/959064.html"},"modified":"2024-12-27T03:37:28","modified_gmt":"2024-12-26T19:37:28","slug":"python%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e7%bb%93%e6%9e%84%e4%bd%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/959064.html","title":{"rendered":"python\u5982\u4f55\u8868\u793a\u7ed3\u6784\u4f53"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102009\/eb73c5df-5682-47a5-b2df-8677dfe36e1e.webp\" alt=\"python\u5982\u4f55\u8868\u793a\u7ed3\u6784\u4f53\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u7ed3\u6784\u4f53\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8868\u793a\uff0c\u5982\u4f7f\u7528\u5b57\u5178\u3001\u547d\u540d\u5143\u7ec4\uff08namedtuple\uff09\u3001\u6570\u636e\u7c7b\uff08dataclasses\uff09\u3001\u81ea\u5b9a\u4e49\u7c7b\u7b49\u3002\u6570\u636e\u7c7b\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u66f4\u591a\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u9ed8\u8ba4\u503c\u3001\u7c7b\u578b\u68c0\u67e5\u548c\u81ea\u52a8\u751f\u6210\u7684\u7279\u6b8a\u65b9\u6cd5\uff08\u5982<code>__init__<\/code>\u548c<code>__repr__<\/code>\uff09\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u6570\u636e\u7c7b\uff08dataclasses\uff09\u662f\u8868\u793a\u7ed3\u6784\u4f53\u7684\u4e00\u79cd\u63a8\u8350\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u5f3a\u5927\u7684\u529f\u80fd\u3002\u6570\u636e\u7c7b\u662fPython 3.7\u5f15\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u901a\u8fc7\u88c5\u9970\u5668\u548c\u7c7b\u578b\u63d0\u793a\u6765\u7b80\u5316\u7c7b\u7684\u5b9a\u4e49\u3002\u4f7f\u7528\u6570\u636e\u7c7b\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u5e76\u81ea\u52a8\u751f\u6210\u521d\u59cb\u5316\u65b9\u6cd5\u548c\u5176\u4ed6\u6709\u7528\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u4e0d\u540c\u65b9\u5f0f\u8868\u793a\u7ed3\u6784\u4f53\u7684\u4f18\u7f3a\u70b9\u548c\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u8868\u793a\u7ed3\u6784\u4f53<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u6a21\u62df\u7ed3\u6784\u4f53\uff0c\u4f46\u5b83\u7f3a\u4e4f\u7c7b\u578b\u5b89\u5168\u548c\u81ea\u52a8\u8865\u5168\u7b49\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = {<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;age&quot;: 30,<\/p>\n<p>    &quot;city&quot;: &quot;New York&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u4f18\u70b9\u662f\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u52a8\u6001\u6dfb\u52a0\u6216\u5220\u9664\u952e\u503c\u5bf9\u3002\u7136\u800c\uff0c\u5b57\u5178\u7684\u7f3a\u70b9\u662f\u6ca1\u6709\u7c7b\u578b\u7ea6\u675f\uff0c\u5bb9\u6613\u51fa\u9519\uff0c\u5c24\u5176\u662f\u5728\u5927\u578b\u9879\u76ee\u4e2d\u3002\u6b64\u5916\uff0c\u5b57\u5178\u7684\u5c5e\u6027\u8bbf\u95ee\u76f8\u5bf9\u7e41\u7410\uff0c\u9700\u8981\u4f7f\u7528<code>[]<\/code>\u64cd\u4f5c\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u547d\u540d\u5143\u7ec4\uff08namedtuple\uff09\u8868\u793a\u7ed3\u6784\u4f53<\/p>\n<\/p>\n<p><p>\u547d\u540d\u5143\u7ec4\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u5bf9\u8c61\u7c7b\u578b\uff0c\u6bd4\u5b57\u5178\u66f4\u63a5\u8fd1\u7ed3\u6784\u4f53\u7684\u6982\u5ff5\u3002\u5b83\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027\uff0c\u540c\u65f6\u5141\u8bb8\u4f7f\u7528\u5c5e\u6027\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import namedtuple<\/p>\n<p>Person = namedtuple(&#39;Person&#39;, [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;])<\/p>\n<p>person = Person(name=&quot;Alice&quot;, age=30, city=&quot;New York&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u547d\u540d\u5143\u7ec4\u7684\u4f18\u70b9\u5305\u62ec\uff1a\u901a\u8fc7\u5c5e\u6027\u540d\u8bbf\u95ee\u5143\u7d20\u3001\u5185\u5b58\u5360\u7528\u5c0f\u3001\u5177\u5907\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027\u3002\u7f3a\u70b9\u662f\u4e0d\u80fd\u8f7b\u6613\u6dfb\u52a0\u6216\u5220\u9664\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u6570\u636e\u7c7b\uff08dataclasses\uff09\u8868\u793a\u7ed3\u6784\u4f53<\/p>\n<\/p>\n<p><p>\u6570\u636e\u7c7b\u662fPython 3.7\u5f15\u5165\u7684\u529f\u80fd\uff0c\u65e8\u5728\u7b80\u5316\u7c7b\u7684\u5b9a\u4e49\uff0c\u7279\u522b\u662f\u7528\u4e8e\u5b58\u50a8\u6570\u636e\u7684\u7c7b\u3002\u6570\u636e\u7c7b\u53ef\u4ee5\u81ea\u52a8\u751f\u6210<code>__init__<\/code>\u3001<code>__repr__<\/code>\u7b49\u65b9\u6cd5\uff0c\u5e76\u652f\u6301\u7c7b\u578b\u63d0\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dataclasses import dataclass<\/p>\n<p>@dataclass<\/p>\n<p>class Person:<\/p>\n<p>    name: str<\/p>\n<p>    age: int<\/p>\n<p>    city: str<\/p>\n<p>person = Person(name=&quot;Alice&quot;, age=30, city=&quot;New York&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6570\u636e\u7c7b\u7684\u4f18\u70b9\u5305\u62ec\uff1a\u7b80\u6d01\u7684\u8bed\u6cd5\u3001\u652f\u6301\u7c7b\u578b\u63d0\u793a\u3001\u81ea\u52a8\u751f\u6210\u5e38\u7528\u65b9\u6cd5\u3002\u6570\u636e\u7c7b\u8fd8\u652f\u6301\u9ed8\u8ba4\u503c\u548c\u53ef\u53d8\/\u4e0d\u53ef\u53d8\u8bbe\u7f6e\u3002\u5b83\u662f\u8868\u793a\u7ed3\u6784\u4f53\u7684\u9996\u9009\u65b9\u5f0f\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b\u8868\u793a\u7ed3\u6784\u4f53<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u81ea\u5b9a\u4e49\u7c7b\u662f\u521b\u5efa\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u5e38\u7528\u65b9\u5f0f\u3002\u4e0e\u6570\u636e\u7c7b\u76f8\u6bd4\uff0c\u81ea\u5b9a\u4e49\u7c7b\u9700\u8981\u624b\u52a8\u5b9a\u4e49<code>__init__<\/code>\u65b9\u6cd5\u4ee5\u53ca\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age, city):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>        self.city = city<\/p>\n<p>person = Person(name=&quot;Alice&quot;, age=30, city=&quot;New York&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u7c7b\u7684\u4f18\u70b9\u662f\u9ad8\u5ea6\u7075\u6d3b\uff0c\u9002\u5408\u9700\u8981\u590d\u6742\u884c\u4e3a\u7684\u6570\u636e\u7ed3\u6784\u3002\u7f3a\u70b9\u662f\u5b9a\u4e49\u8f83\u4e3a\u7e41\u7410\uff0c\u5c24\u5176\u662f\u9700\u8981\u7f16\u5199\u91cd\u590d\u4ee3\u7801\uff08\u5982\u521d\u59cb\u5316\u65b9\u6cd5\uff09\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7efc\u5408\u6bd4\u8f83\u4e0e\u9009\u62e9<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5982\u4f55\u8868\u793a\u7ed3\u6784\u4f53\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u9879\u76ee\u89c4\u6a21\u505a\u51fa\u51b3\u7b56\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b57\u5178<\/strong>\u9002\u5408\u7b80\u5355\u3001\u4e34\u65f6\u7684\u6570\u636e\u5b58\u50a8\uff0c\u4f46\u5728\u5927\u578b\u9879\u76ee\u4e2d\u4e0d\u63a8\u8350\u4f7f\u7528\u3002<\/li>\n<li><strong>\u547d\u540d\u5143\u7ec4<\/strong>\u9002\u5408\u8f7b\u91cf\u7ea7\u3001\u4e0d\u53d8\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u8282\u7701\u5185\u5b58\u65f6\u3002<\/li>\n<li><strong>\u6570\u636e\u7c7b<\/strong>\u662f\u73b0\u4ee3Python\u9879\u76ee\u4e2d\u8868\u793a\u7ed3\u6784\u4f53\u7684\u63a8\u8350\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002<\/li>\n<li><strong>\u81ea\u5b9a\u4e49\u7c7b<\/strong>\u9002\u5408\u9700\u8981\u590d\u6742\u884c\u4e3a\u548c\u65b9\u6cd5\u7684\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5e94\u4f18\u5148\u8003\u8651\u6570\u636e\u7c7b\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u826f\u597d\u7684\u53ef\u8bfb\u6027\u3001\u7c7b\u578b\u5b89\u5168\u548c\u81ea\u52a8\u5316\u652f\u6301\uff0c\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u7ed3\u6784\u4f53\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>dataclasses<\/code>\u6a21\u5757\u6765\u521b\u5efa\u7ed3\u6784\u4f53\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u5e76\u4f7f\u7528<code>@dataclass<\/code>\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u5177\u6709\u5c5e\u6027\u7684\u7ed3\u6784\u4f53\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from dataclasses import dataclass\n\n@dataclass\nclass Person:\n    name: str\n    age: int\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u5bf9\u8c61\u5e76\u8bbf\u95ee\u5176\u5c5e\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u7ed3\u6784\u4f53\u4e0e\u7c7b\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>\u7ed3\u6784\u4f53\u901a\u5e38\u7528\u4e8e\u5b58\u50a8\u7b80\u5355\u7684\u6570\u636e\uff0c\u800c\u7c7b\u5219\u53ef\u4ee5\u5305\u542b\u66f4\u590d\u6742\u7684\u884c\u4e3a\u548c\u65b9\u6cd5\u3002\u7ed3\u6784\u4f53\u4e3b\u8981\u5173\u6ce8\u6570\u636e\u7684\u5b58\u50a8\uff0c\u800c\u7c7b\u5219\u4e0d\u4ec5\u5173\u6ce8\u6570\u636e\uff0c\u8fd8\u5173\u6ce8\u6570\u636e\u64cd\u4f5c\u3002\u4f7f\u7528<code>dataclass<\/code>\u53ef\u4ee5\u8ba9\u7ed3\u6784\u4f53\u5177\u5907\u4e00\u4e9b\u7c7b\u7684\u7279\u6027\uff0c\u4f8b\u5982\u65b9\u6cd5\u548c\u7ee7\u627f\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684\u547d\u540d\u5143\u7ec4\u6765\u5b9e\u73b0\u7ed3\u6784\u4f53\uff1f<\/strong><br \/>\u547d\u540d\u5143\u7ec4\u662f\u53e6\u4e00\u79cd\u8868\u793a\u7ed3\u6784\u4f53\u7684\u65b9\u5f0f\uff0c\u4f7f\u7528<code>collections.namedtuple<\/code>\u6765\u5b9a\u4e49\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import namedtuple\n\nPerson = namedtuple(&#39;Person&#39;, [&#39;name&#39;, &#39;age&#39;])\njohn = Person(name=&#39;John&#39;, age=30)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u5c5e\u6027\u540d\u79f0\u800c\u4e0d\u662f\u7d22\u5f15\u6765\u8bbf\u95ee\u6570\u636e\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7ed3\u6784\u4f53\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8868\u793a\uff0c\u5982\u4f7f\u7528\u5b57\u5178\u3001\u547d\u540d\u5143\u7ec4\uff08namedtuple\uff09\u3001\u6570\u636e\u7c7b\uff08datac [&hellip;]","protected":false},"author":3,"featured_media":959072,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959064"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=959064"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959064\/revisions"}],"predecessor-version":[{"id":959075,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959064\/revisions\/959075"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/959072"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=959064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=959064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=959064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}