{"id":1084520,"date":"2025-01-08T13:08:05","date_gmt":"2025-01-08T05:08:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1084520.html"},"modified":"2025-01-08T13:08:08","modified_gmt":"2025-01-08T05:08:08","slug":"python%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e8%87%aa%e5%ae%9a%e4%b9%89%e7%bb%93%e6%9e%84%e4%bd%93-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1084520.html","title":{"rendered":"Python\u5982\u4f55\u521b\u5efa\u81ea\u5b9a\u4e49\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\/24194837\/a641b576-0b56-4f51-9185-bbdcc49bed40.webp\" alt=\"Python\u5982\u4f55\u521b\u5efa\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u521b\u5efa\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u4f7f\u7528\u7c7b\u3001namedtuple\u548cdataclass<\/strong>\u3002\u6211\u4eec\u63a5\u4e0b\u6765\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u7c7b\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u7c7b\uff08class\uff09\u521b\u5efa\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u662fPython\u4e2d\u6700\u7075\u6d3b\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u7c7b\u5141\u8bb8\u4f60\u5b9a\u4e49\u6570\u636e\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u63d0\u4f9b\u4e86\u5bf9\u7ed3\u6784\u4f53\u7684\u9ad8\u5ea6\u81ea\u5b9a\u4e49\u548c\u6269\u5c55\u6027\u3002\u901a\u8fc7\u7c7b\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u6784\u9020\u51fd\u6570\u3001\u6dfb\u52a0\u9ed8\u8ba4\u503c\u3001\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7c7b\uff08class\uff09\u521b\u5efa\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u7684\u65b9\u5f0f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u7c7b\u6765\u5b9a\u4e49\u5305\u542b\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u7ed3\u6784\u4f53\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age, em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>        self.email = email<\/p>\n<h2><strong>\u521b\u5efaPerson\u5bf9\u8c61<\/strong><\/h2>\n<p>person = Person(&quot;John Doe&quot;, 30, &quot;john.doe@example.com&quot;)<\/p>\n<h2><strong>\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027<\/strong><\/h2>\n<p>print(person.name)  # \u8f93\u51fa\uff1aJohn Doe<\/p>\n<p>print(person.age)   # \u8f93\u51fa\uff1a30<\/p>\n<p>print(person.email) # \u8f93\u51fa\uff1ajohn.doe@example.com<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>Person<\/code>\u7684\u7c7b\uff0c\u8be5\u7c7b\u5177\u6709\u4e09\u4e2a\u5c5e\u6027\uff1a<code>name<\/code>\u3001<code>age<\/code>\u548c<code>email<\/code>\u3002\u901a\u8fc7<code>__init__<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u521d\u59cb\u5316\u8fd9\u4e9b\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528namedtuple\u521b\u5efa\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53<\/h3>\n<\/p>\n<p><p>namedtuple\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u4f4d\u4e8e<code>collections<\/code>\u6a21\u5757\u4e2d\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u3001\u4e0d\u53ef\u53d8\u7684\u7ed3\u6784\u4f53\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import namedtuple<\/p>\n<h2><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53<\/strong><\/h2>\n<p>Person = namedtuple(&quot;Person&quot;, [&quot;name&quot;, &quot;age&quot;, &quot;email&quot;])<\/p>\n<h2><strong>\u521b\u5efaPerson\u5bf9\u8c61<\/strong><\/h2>\n<p>person = Person(&quot;John Doe&quot;, 30, &quot;john.doe@example.com&quot;)<\/p>\n<h2><strong>\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027<\/strong><\/h2>\n<p>print(person.name)  # \u8f93\u51fa\uff1aJohn Doe<\/p>\n<p>print(person.age)   # \u8f93\u51fa\uff1a30<\/p>\n<p>print(person.email) # \u8f93\u51fa\uff1ajohn.doe@example.com<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>namedtuple<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>Person<\/code>\u7684\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e09\u4e2a\u5b57\u6bb5\uff1a<code>name<\/code>\u3001<code>age<\/code>\u548c<code>email<\/code>\u3002\u4e0e\u7c7b\u4e0d\u540c\uff0cnamedtuple\u521b\u5efa\u7684\u5bf9\u8c61\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u66f4\u6539\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528dataclass\u521b\u5efa\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53<\/h3>\n<\/p>\n<p><p>dataclass\u662fPython 3.7\u5f15\u5165\u7684\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u4f4d\u4e8e<code>dataclasses<\/code>\u6a21\u5757\u4e2d\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u6570\u636e\u7c7b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/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>    email: str<\/p>\n<h2><strong>\u521b\u5efaPerson\u5bf9\u8c61<\/strong><\/h2>\n<p>person = Person(&quot;John Doe&quot;, 30, &quot;john.doe@example.com&quot;)<\/p>\n<h2><strong>\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027<\/strong><\/h2>\n<p>print(person.name)  # \u8f93\u51fa\uff1aJohn Doe<\/p>\n<p>print(person.age)   # \u8f93\u51fa\uff1a30<\/p>\n<p>print(person.email) # \u8f93\u51fa\uff1ajohn.doe@example.com<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>@dataclass<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>Person<\/code>\u7684\u7c7b\uff0c\u5e76\u6dfb\u52a0\u4e86\u4e09\u4e2a\u5c5e\u6027\uff1a<code>name<\/code>\u3001<code>age<\/code>\u548c<code>email<\/code>\u3002dataclass\u4f1a\u81ea\u52a8\u751f\u6210<code>__init__<\/code>\u3001<code>__repr__<\/code>\u548c\u5176\u4ed6\u65b9\u6cd5\uff0c\u4f7f\u5f97\u5b9a\u4e49\u6570\u636e\u7c7b\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7c7b\uff08class\uff09\u521b\u5efa\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u7c7b\u521b\u5efa\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u65f6\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u66f4\u591a\u7684\u529f\u80fd\uff0c\u5982\u6570\u636e\u9a8c\u8bc1\u3001\u9ed8\u8ba4\u503c\u548c\u81ea\u5b9a\u4e49\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u9ad8\u7ea7\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age, email=&quot;unknown@example.com&quot;):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>        self.email = email<\/p>\n<p>    # \u6570\u636e\u9a8c\u8bc1<\/p>\n<p>    @property<\/p>\n<p>    def age(self):<\/p>\n<p>        return self._age<\/p>\n<p>    @age.setter<\/p>\n<p>    def age(self, value):<\/p>\n<p>        if value &lt; 0:<\/p>\n<p>            raise ValueError(&quot;Age cannot be negative&quot;)<\/p>\n<p>        self._age = value<\/p>\n<p>    # \u81ea\u5b9a\u4e49\u65b9\u6cd5<\/p>\n<p>    def greet(self):<\/p>\n<p>        return f&quot;Hello, my name is {self.name}.&quot;<\/p>\n<h2><strong>\u521b\u5efaPerson\u5bf9\u8c61<\/strong><\/h2>\n<p>person = Person(&quot;John Doe&quot;, 30)<\/p>\n<h2><strong>\u8bbf\u95ee\u5bf9\u8c61\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong><\/h2>\n<p>print(person.name)  # \u8f93\u51fa\uff1aJohn Doe<\/p>\n<p>print(person.age)   # \u8f93\u51fa\uff1a30<\/p>\n<p>print(person.email) # \u8f93\u51fa\uff1aunknown@example.com<\/p>\n<p>print(person.greet())  # \u8f93\u51fa\uff1aHello, my name is John Doe.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u503c<code>email<\/code>\uff0c\u5e76\u4f7f\u7528\u5c5e\u6027\uff08property\uff09\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\uff0c\u786e\u4fdd<code>age<\/code>\u4e0d\u80fd\u4e3a\u8d1f\u6570\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u65b9\u6cd5<code>greet<\/code>\uff0c\u7528\u4e8e\u751f\u6210\u95ee\u5019\u8bed\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u521b\u5efa\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528\u7c7b\uff08class\uff09\u3001namedtuple\u548cdataclass\u3002\u7c7b\u63d0\u4f9b\u4e86\u6700\u5927\u7684\u7075\u6d3b\u6027\u548c\u81ea\u5b9a\u4e49\u80fd\u529b<\/strong>\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u65b9\u6cd5\u3001\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u548c\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002\u800cnamedtuple\u548cdataclass\u5219\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u9002\u7528\u4e8e\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u521b\u5efa\u548c\u7ba1\u7406\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7c7b\u6765\u5b9e\u73b0\u3002\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u5e76\u4f7f\u7528<code>__init__<\/code>\u65b9\u6cd5\u6765\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u7684\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u8868\u793a\u70b9\u7684\u7ed3\u6784\u4f53\u53ef\u4ee5\u5982\u4e0b\u5b9a\u4e49\uff1a<\/p>\n<pre><code class=\"language-python\">class Point:\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a<code>Point<\/code>\u5bf9\u8c61\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u5177\u6709<code>x<\/code>\u548c<code>y<\/code>\u5c5e\u6027\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7684<code>collections.namedtuple<\/code>\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u4f7f\u7528<code>collections.namedtuple<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u3002\u4e0e\u4f20\u7edf\u7c7b\u76f8\u6bd4\uff0c<code>namedtuple<\/code>\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u4e0d\u53ef\u53d8\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import namedtuple\n\nPoint = namedtuple(&#39;Point&#39;, [&#39;x&#39;, &#39;y&#39;])\np = Point(1, 2)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u800c\u4e14\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u4e2d\u6dfb\u52a0\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728Python\u7684\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u4e2d\u6dfb\u52a0\u65b9\u6cd5\u4e0e\u6dfb\u52a0\u5c5e\u6027\u7c7b\u4f3c\u3002\u53ef\u4ee5\u5728\u7c7b\u4e2d\u5b9a\u4e49\u51fd\u6570\uff0c\u4f7f\u7528<code>self<\/code>\u53c2\u6570\u6765\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">class Rectangle:\n    def __init__(self, width, height):\n        self.width = width\n        self.height = height\n\n    def area(self):\n        return self.width * self.height\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<code>Rectangle<\/code>\u5bf9\u8c61\u5e76\u8c03\u7528<code>area<\/code>\u65b9\u6cd5\u6765\u8ba1\u7b97\u9762\u79ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u521b\u5efa\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u4f7f\u7528\u7c7b\u3001namedtuple\u548cdataclass\u3002\u6211\u4eec\u63a5\u4e0b\u6765\u8be6\u7ec6\u4ecb [&hellip;]","protected":false},"author":3,"featured_media":1084534,"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\/1084520"}],"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=1084520"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084520\/revisions"}],"predecessor-version":[{"id":1084535,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1084520\/revisions\/1084535"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1084534"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1084520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1084520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1084520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}