{"id":204365,"date":"2025-05-29T12:49:35","date_gmt":"2025-05-29T04:49:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204365\/"},"modified":"2025-05-29T12:49:35","modified_gmt":"2025-05-29T04:49:35","slug":"python-%e4%b8%ad%e5%8a%a0%e5%bc%95%e5%8f%b7%e7%9a%84-typing-hint-%e7%a9%b6%e7%ab%9f%e6%98%af%e5%a6%82%e4%bd%95%e5%b7%a5%e4%bd%9c%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204365\/","title":{"rendered":"Python \u4e2d\u52a0\u5f15\u53f7\u7684 Typing Hint \u7a76\u7adf\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u4e2d\u52a0\u5f15\u53f7\u7684 Typing Hint \u7a76\u7adf\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aPython \u4e2d\u52a0\u5f15\u53f7\u7684 Typing Hint \u7a76\u7adf\u662f\u5982\u4f55\u5de5\u4f5c\u7684\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\/20241105\/17308049846729fcf8c3b7b.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u52a0\u5f15\u53f7\u7684 typing hint \u7684\u539f\u7406<\/strong><\/p>\n<p>\u5728 python \u4e2d\uff0c\u6709\u4e9b\u7c7b\u578b\u6807\u6ce8\u5728\u4f7f\u7528\u65f6\u53ef\u80fd\u672a\u88ab\u5b8c\u5168\u5b9a\u4e49\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u91c7\u7528\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u7c7b\u578b\u6807\u6ce8\u3002<\/p>\n<p>pep 484 \u5f15\u5165\u4e86\u8fd9\u79cd\u7279\u6027\u3002\u5f53\u7c7b\u578b\u6807\u6ce8\u5305\u542b\u5c1a\u672a\u5b9a\u4e49\u7684\u540d\u79f0\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u6765\u8868\u793a\u8be5\u5b9a\u4e49\uff0c\u4ee5\u4fbf\u7a0d\u540e\u89e3\u6790\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>@classmethod\ndef from_orm(cls: Type['Model'], obj: Any) -&gt; 'Model':<\/pre>\n<p>\u6b64\u4ee3\u7801\u4e2d\u7684 type[&#8216;model&#8217;] \u8868\u793a model \u7c7b\u7684\u7c7b\u578b\uff0c\u4f46 model \u7c7b\u5728\u65b9\u6cd5\u7684\u53c2\u6570\u7c7b\u578b\u6807\u6ce8\u4e2d\u5c1a\u672a\u88ab\u5b9a\u4e49\u3002\u56e0\u6b64\u4f7f\u7528\u4e86\u5b57\u7b26\u4e32\u5b57\u9762\u91cf &#8216;model&#8217;\u3002<\/p>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5373\u4f7f\u4f7f\u7528\u4e86\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7684\u7c7b\u578b\u6807\u6ce8\uff0c\u4ee3\u7801\u667a\u80fd\u63d0\u793a\u4ecd\u7136\u53ef\u4ee5\u5728 visual studio code \u4e2d\u6b63\u5e38\u5de5\u4f5c\u3002\u8fd9\u662f\u56e0\u4e3a\u7c7b\u578b\u6807\u6ce8\u5728\u4f7f\u7528\u524d\u4f1a\u7ecf\u8fc7\u5ef6\u8fdf\u6c42\u503c\uff0c\u4ece\u800c\u83b7\u5f97\u5b9e\u9645\u7684\u6807\u6ce8\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5f15\u5165\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7c7b\u578b\u6807\u6ce8\u7684\u539f\u56e0<\/strong><\/p>\n<p>\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7c7b\u578b\u7684\u5f15\u5165\u662f\u4e3a\u4e86\u89e3\u51b3\u67d0\u4e9b\u517c\u5bb9\u6027\u95ee\u9898\uff0c\u7279\u522b\u662f\u4e0e python 3.6 \u4e4b\u524d\u7684\u7248\u672c\u517c\u5bb9\u3002\u5728 python 3.6 \u4e4b\u524d\uff0c\u7c7b\u578b\u6807\u6ce8\u5fc5\u987b\u662f\u5df2\u7ecf\u5b9a\u4e49\u7684\u6807\u8bc6\u7b26\u3002\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7c7b\u578b\u6807\u6ce8\u5141\u8bb8\u5728\u7c7b\u7ed3\u675f\u524d\u5b9a\u4e49\u7c7b\u578b\u6807\u6ce8\u3002<\/p>\n<p><strong>pep 563 \u4e2d\u7684\u53d8\u5316<\/strong><\/p>\n<p>pep 563 \u8fdb\u4e00\u6b65\u6539\u8fdb\u4e86\u7c7b\u578b\u6807\u6ce8\uff0c\u65e0\u8bba\u6807\u6ce8\u662f\u5426\u5199\u6210\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u5b9e\u9645\u90fd\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u8fd9\u4f7f\u5f97\u53ef\u4ee5\u4f7f\u7528 model \u800c\u65e0\u9700\u4f7f\u7528 &#8216;model&#8217;\uff0c\u56e0\u4e3a\u4e0d\u518d\u9700\u8981 model \u5728\u6807\u6ce8\u65f6\u5c31\u5fc5\u987b\u662f\u4e00\u4e2a\u5df2\u5b9a\u4e49\u7684\u6807\u8bc6\u7b26\u3002<\/p>\n<p>\u901a\u8fc7 from __future__ import annotations \u5bfc\u5165\uff0c\u53ef\u4ee5\u63d0\u524d\u542f\u7528 pep 563 \u4e2d\u7684\u8fd9\u4e9b\u529f\u80fd\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aPython \u4e2d\u52a0\u5f15\u53f7\u7684 Typing Hint \u7a76\u7adf\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u4e2d\u52a0\u5f15\u53f7\u7684 Typ&#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-204365","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204365","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=204365"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204365\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}