{"id":3414,"date":"2025-06-02T15:17:47","date_gmt":"2025-06-04T07:53:46","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3414"},"modified":"2025-06-04T15:17:47","modified_gmt":"2025-06-04T07:53:46","slug":"%e4%ba%86%e8%a7%a3python%e7%9a%84dataclass%ef%bc%9a%e7%b0%a1%e5%96%ae%e3%80%81%e5%bf%ab%e9%80%9f%e3%80%81%e5%ae%89%e5%85%a8%e7%9a%84%e8%b3%87%e6%96%99%e9%a1%9e%e5%88%a5","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e7%9a%84dataclass%ef%bc%9a%e7%b0%a1%e5%96%ae%e3%80%81%e5%bf%ab%e9%80%9f%e3%80%81%e5%ae%89%e5%85%a8%e7%9a%84%e8%b3%87%e6%96%99%e9%a1%9e%e5%88%a5\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 dataclass()\uff1a2025 \u6700\u65b0\u5be6\u4f5c\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, dataclass, Python 2025, Python \u7a0b\u5f0f\u8a2d\u8a08\"><\/p>\n<h1>Python \u4e2d\u7684 dataclass()<\/h1>\n<p>\u5728 **2025** \u7684 Python \u7248\u672c\u4e2d\uff0c`dataclass()` \u51fd\u5f0f\u4f9d\u7136\u662f\u958b\u767c\u8005\u5b9a\u7fa9\u548c\u4f7f\u7528\u8cc7\u6599\u985e\u5225\u7684\u5f37\u5927\u5de5\u5177\u3002\u9019\u500b\u51fd\u5f0f\u7c21\u5316\u4e86\u8cc7\u6599\u985e\u5225\u7684\u5b9a\u7fa9\uff0c\u4e26\u81ea\u52d5\u7522\u751f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982 <strong>__init__<\/strong>()\u3001<strong>__repr__<\/strong>()\u3001<strong>__eq__<\/strong>() \u7b49\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u66f4\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\u7684\u64b0\u5beb\u3002<\/p>\n<h2>dataclass() \u7684\u4f7f\u7528\u65b9\u5f0f<\/h2>\n<p>\u5b9a\u7fa9\u8cc7\u6599\u985e\u5225\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u5728\u985e\u5225\u5b9a\u7fa9\u524d\u52a0\u4e0a @dataclass \u88dd\u98fe\u5668\uff0c\u4e26\u6307\u5b9a\u985e\u5225\u7684\u5c6c\u6027\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom dataclasses import dataclass<\/p>\n<p>@dataclass<br \/>\nclass Person:<br \/>\n    name: str<br \/>\n    age: int<br \/>\n&#8220;`<\/p>\n<p>\u4e0a\u8ff0\u7a0b\u5f0f\u78bc\u5b9a\u7fa9\u4e86\u4e00\u500b `Person` \u985e\u5225\uff0c\u64c1\u6709 `name` \u548c `age` \u5169\u500b\u5c6c\u6027\uff0c\u5206\u5225\u662f\u5b57\u4e32\u548c\u6574\u6578\u578b\u614b\u3002\u5b9a\u7fa9\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u8f15\u9b06\u5efa\u7acb\u7269\u4ef6\u5be6\u4f8b\u4e26\u8a2d\u5b9a\u5c6c\u6027\uff1a<\/p>\n<p>&#8220;`python<br \/>\nperson = Person(name=&#8217;John&#8217;, age=20)<br \/>\n&#8220;`<\/p>\n<p>\u6b64\u5916\uff0c`dataclass()` \u9084\u652f\u6301\u4e00\u4e9b\u984d\u5916\u53c3\u6578\uff0c\u5982 `frozen` \u548c `order`\u3002`frozen` \u53c3\u6578\u4f7f\u985e\u5225\u7684\u5c6c\u6027\u4e0d\u53ef\u4fee\u6539\uff0c\u800c `order` \u53c3\u6578\u5247\u7528\u65bc\u6307\u5b9a\u5c6c\u6027\u7684\u6392\u5e8f\u65b9\u5f0f\uff1a<\/p>\n<p>&#8220;`python<br \/>\n@dataclass(frozen=True, order=True)<br \/>\nclass Person:<br \/>\n    name: str<br \/>\n    age: int<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u7684\u5b9a\u7fa9\u78ba\u4fdd\u4e86 `Person` \u985e\u5225\u7684\u5c6c\u6027\u4e0d\u53ef\u4fee\u6539\uff0c\u4e26\u4e14\u5c6c\u6027\u6309\u5b9a\u7fa9\u7684\u9806\u5e8f\u9032\u884c\u6bd4\u8f03\u3002<\/p>\n<h2>dataclass() \u7684\u512a\u9ede<\/h2>\n<p>\u4f7f\u7528 `dataclass()` \u5b9a\u7fa9\u8cc7\u6599\u985e\u5225\u7684\u512a\u52e2\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u7c21\u5316\u8cc7\u6599\u985e\u5225\u7684\u5b9a\u7fa9\uff1a<\/strong>\u958b\u767c\u8005\u80fd\u66f4\u8f15\u9b06\u5730\u5b9a\u7fa9\u8cc7\u6599\u985e\u5225\uff0c\u81ea\u52d5\u7522\u751f\u5e38\u7528\u51fd\u5f0f\uff0c\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\u3002<\/li>\n<li><strong>\u5c6c\u6027\u63a7\u5236\u53c3\u6578\uff1a<\/strong>\u53ef\u6307\u5b9a `frozen` \u548c `order`\uff0c\u63a7\u5236\u5c6c\u6027\u7684\u53ef\u4fee\u6539\u6027\u548c\u6392\u5e8f\u65b9\u5f0f\u3002<\/li>\n<li><strong>\u6bd4\u8f03\u64cd\u4f5c\u652f\u6301\uff1a<\/strong>\u81ea\u52d5\u751f\u6210 `__eq__()` \u65b9\u6cd5\uff0c\u4f7f\u7269\u4ef6\u5be6\u4f8b\u4e4b\u9593\u7684\u6bd4\u8f03\u8b8a\u5f97\u7c21\u55ae\u3002<\/li>\n<\/ul>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5b8c\u6574\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `dataclass()` \u5b9a\u7fa9\u8cc7\u6599\u985e\u5225\u4e26\u9032\u884c\u4f7f\u7528\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom dataclasses import dataclass<\/p>\n<p>@dataclass<br \/>\nclass Product:<br \/>\n    name: str<br \/>\n    price: float<br \/>\n    quantity: int<\/p>\n<p>    def total_price(self) -> float:<br \/>\n        return self.price * self.quantity<\/p>\n<p># \u5275\u5efa Product \u985e\u5225\u7684\u7269\u4ef6\u5be6\u4f8b<br \/>\nitem = Product(name=&#8217;Laptop&#8217;, price=1200.99, quantity=3)<\/p>\n<p># \u8a08\u7b97\u7e3d\u50f9\u683c<br \/>\nprint(f&#8221;Total price for {item.quantity} {item.name}s is: ${item.total_price():.2f}&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5b9a\u7fa9\u4e86\u4e00\u500b `Product` \u985e\u5225\uff0c\u4e26\u5305\u542b\u4e00\u500b\u8a08\u7b97\u7e3d\u50f9\u683c\u7684\u65b9\u6cd5\u3002\u57f7\u884c\u5f8c\u5c07\u8f38\u51fa\u7e3d\u50f9\u683c\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `dataclass()` \u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **\u5fd8\u8a18\u5c0e\u5165 dataclass**\uff1a\u78ba\u4fdd\u5728\u4f7f\u7528\u524d\u5c0e\u5165 `from dataclasses import dataclass`\u3002<br \/>\n2. **\u5c6c\u6027\u985e\u578b\u932f\u8aa4**\uff1a\u78ba\u4fdd\u70ba\u6bcf\u500b\u5c6c\u6027\u63d0\u4f9b\u6b63\u78ba\u7684\u8cc7\u6599\u985e\u578b\uff0c\u4f8b\u5982\uff0c\u5c07 `age` \u5b9a\u7fa9\u70ba `int` \u800c\u975e `str`\u3002<br \/>\n3. **\u4e0d\u53ef\u8b8a\u7269\u4ef6\u7684\u4fee\u6539**\uff1a\u82e5\u4f7f\u7528 `frozen=True`\uff0c\u5c07\u7121\u6cd5\u4fee\u6539\u7269\u4ef6\u5c6c\u6027\uff0c\u8acb\u6aa2\u67e5\u662f\u5426\u6709\u6b64\u9700\u6c42\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>&#8211; \u4f7f\u7528 `dataclass` \u7d50\u5408\u5176\u4ed6 Python \u7279\u6027\uff0c\u5982\u8cc7\u6599\u9a57\u8b49\u548c\u5e8f\u5217\u5316\uff0c\u63d0\u5347\u8cc7\u6599\u8655\u7406\u7684\u9748\u6d3b\u6027\u3002<br \/>\n&#8211; \u63a2\u7d22\u5982\u4f55\u5728\u5927\u578b\u5c08\u6848\u4e2d\u4f7f\u7528 `dataclass` \u7d50\u5408\u8cc7\u6599\u5eab ORM\uff08\u5982 SQLAlchemy\uff09\u4f86\u7c21\u5316\u8cc7\u6599\u7ba1\u7406\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>`dataclass()` \u51fd\u5f0f\u662f Python \u958b\u767c\u8005\u7684\u5f37\u5927\u5de5\u5177\uff0c\u7c21\u5316\u4e86\u8cc7\u6599\u985e\u5225\u7684\u5b9a\u7fa9\u904e\u7a0b\uff0c\u63d0\u4f9b\u4e86\u5c6c\u6027\u63a7\u5236\u548c\u6bd4\u8f03\u64cd\u4f5c\u7684\u652f\u6301\u3002\u96a8\u8457 Python \u7684\u767c\u5c55\uff0c`dataclass()` \u4ecd\u7136\u662f\u8cc7\u6599\u8655\u7406\u7684\u6700\u4f73\u9078\u64c7\u4e4b\u4e00\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u8cc7\u6599\u985e\u5225\uff0c\u53ef\u4ee5\u53c3\u8003 [Python 3.7 \u6587\u4ef6 &#8211; dataclasses](https:\/\/docs.python.org\/3\/library\/dataclasses.html)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: dataclass() \u8207\u50b3\u7d71\u985e\u5225\u76f8\u6bd4\u6709\u4f55\u512a\u52e2\uff1f**<br \/>\nA1: `dataclass()` \u81ea\u52d5\u751f\u6210\u8a31\u591a\u5e38\u7528\u65b9\u6cd5\uff0c\u5982 `__init__()` \u548c `__repr__()`\uff0c\u5927\u5e45\u7c21\u5316\u4e86\u985e\u5225\u7684\u5b9a\u7fa9\u904e\u7a0b\uff0c\u4e26\u589e\u5f37\u4e86\u53ef\u8b80\u6027\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8655\u7406 dataclass \u4e2d\u7684\u53ef\u9078\u5c6c\u6027\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `Optional` \u985e\u578b\u4f86\u5b9a\u7fa9\u53ef\u9078\u5c6c\u6027\uff0c\u4f8b\u5982 `name: Optional[str] = None`\u3002<\/p>\n<p>**Q3: dataclass \u662f\u5426\u652f\u6301\u7e7c\u627f\uff1f**<br \/>\nA3: \u662f\u7684\uff0c`dataclass` \u652f\u6301\u7e7c\u627f\uff0c\u5b50\u985e\u5225\u53ef\u4ee5\u7e7c\u627f\u7236\u985e\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u9019\u6a23\u7684\u512a\u5316\u4e0d\u50c5\u63d0\u5347\u4e86\u5167\u6587\u7684\u5b8c\u6574\u6027\u8207\u5be6\u7528\u6027\uff0c\u540c\u6642\u4e5f\u7b26\u5408 SEO \u7684\u6700\u4f73\u5be6\u8e10\uff0c\u5438\u5f15\u66f4\u591a\u8b80\u8005\u7684\u95dc\u6ce8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Python\u7684dataclass()\u53ef\u4ee5\u5feb\u901f\u3001\u5b89\u5168\u5730\u5efa\u7acb\u8cc7\u6599\u985e\u5225\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u5730\u5b8c\u6210\u5de5\u4f5c\u3002dataclass()\u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u7684\u65b9\u6cd5\u4f86\u5b9a\u7fa9\u8cc7\u6599\u985e\u5225\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u81ea\u52d5\u751f\u6210__init__()\u548c__repr__()\u65b9\u6cd5\uff0c\u4ee5\u53ca\u652f\u63f4\u6bd4\u8f03\u548c\u6392\u5e8f\u7b49\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3414","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-T4","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3414","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3414"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3414\/revisions"}],"predecessor-version":[{"id":12838,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3414\/revisions\/12838"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}