{"id":204469,"date":"2025-05-29T08:23:31","date_gmt":"2025-05-29T00:23:31","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204469\/"},"modified":"2025-05-29T08:23:31","modified_gmt":"2025-05-29T00:23:31","slug":"python-%e4%b8%ad%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bd%bf%e7%94%a8%e5%b8%a6%e5%bc%95%e5%8f%b7%e7%9a%84%e7%b1%bb%e5%9e%8b%e6%a0%87%e6%b3%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204469\/","title":{"rendered":"Python \u4e2d\u4e3a\u4ec0\u4e48\u4f7f\u7528\u5e26\u5f15\u53f7\u7684\u7c7b\u578b\u6807\u6ce8\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u4e2d\u4e3a\u4ec0\u4e48\u4f7f\u7528\u5e26\u5f15\u53f7\u7684\u7c7b\u578b\u6807\u6ce8\uff1f<\/h1>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aPython \u4e2d\u4e3a\u4ec0\u4e48\u4f7f\u7528\u5e26\u5f15\u53f7\u7684\u7c7b\u578b\u6807\u6ce8\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241108\/1731053752672dc8b895e19.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u4e2d\u52a0\u4e0a\u5f15\u53f7\u7684 typing hint<\/strong><\/p>\n<p>\u5f53\u4f7f\u7528 type hint \u6807\u6ce8\u7c7b\u578b\u5c1a\u672a\u5b8c\u5168\u5b9a\u4e49\u65f6\uff0cpython \u5141\u8bb8\u4f7f\u7528\u5355\u5f15\u53f7\u62ec\u8d77\u6765\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u8003\u8651\u4e00\u4e2a pydantic \u6a21\u578b\uff1a<\/p>\n<pre>class model:\n    ...<\/pre>\n<p>\u5728\u5b83\u7684\u65b9\u6cd5\u4e2d\uff0c\u60f3\u8981\u6307\u5b9a\u53c2\u6570\u4e3a model \u7c7b\u578b\uff1a<\/p>\n<pre>def from_orm(obj: \"Model\") -&gt; \"Model\":\n    ...<\/pre>\n<p>\u6b64\u65f6\uff0cmodel \u5c1a\u672a\u5b8c\u5168\u5b9a\u4e49\u3002\u901a\u8fc7\u4f7f\u7528\u5f15\u53f7\uff0cpython \u5ef6\u8fdf\u89e3\u6790\uff0c\u76f4\u5230\u4ee3\u7801\u5b9e\u9645\u6267\u884c\u65f6\u518d\u786e\u5b9a\u7c7b\u578b\u3002<\/p>\n<p><strong>\u4e3a\u4f55\u4f7f\u7528\u5f15\u53f7\uff1f<\/strong><\/p>\n<p>\u4e0e\u517c\u5bb9\u4f4e\u7248\u672c python \u65e0\u5173\u3002\u8fd9\u662f python 3.7 \u5f15\u5165\u7684 pep 484 \u7279\u6027\uff0c\u5141\u8bb8\u4f7f\u7528\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u7c7b\u578b\u6807\u6ce8\uff0c\u4ee5\u4fbf <strong>\u672a\u6765\u786e\u5b9a\u7c7b\u578b<\/strong>\u3002<\/p>\n<p><strong>\u672a\u6765\u786e\u5b9a\u7c7b\u578b<\/strong><\/p>\n<p>\u5728\u5f15\u5165 pep 563 \u4e4b\u540e\uff0c\u672a\u6765\u786e\u5b9a\u7c7b\u578b\u6210\u4e3a python \u7684\u9ed8\u8ba4\u884c\u4e3a\u3002\u8fd9\u610f\u5473\u7740\u65e0\u8bba\u7c7b\u578b\u6807\u6ce8\u662f\u5426\u4f7f\u7528\u5f15\u53f7\uff0c\u5b9e\u9645\u7c7b\u578b\u90fd\u9996\u5148\u89e3\u6790\u4e3a\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002\u8fd9\u907f\u514d\u4e86\u4f9d\u8d56\u987a\u5e8f\u95ee\u9898\u3002<\/p>\n<p>\u5c06\u6765\uff0cfrom __future__ import annotations \u6a21\u5757\u5c06\u5728 python \u4e2d\u542f\u7528\u6b64\u884c\u4e3a\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u5f15\u53f7\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aPython \u4e2d\u4e3a\u4ec0\u4e48\u4f7f\u7528\u5e26\u5f15\u53f7\u7684\u7c7b\u578b\u6807\u6ce8\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u4e2d\u4e3a\u4ec0\u4e48\u4f7f\u7528\u5e26\u5f15\u53f7&#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-204469","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204469","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=204469"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204469\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}