{"id":49561,"date":"2024-12-03T09:37:02","date_gmt":"2024-12-03T01:37:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49561\/"},"modified":"2024-12-03T09:37:02","modified_gmt":"2024-12-03T01:37:02","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-type-%e4%b8%ba-python-%e7%b1%bb%e6%8f%90%e4%be%9b%e7%b2%be%e7%a1%ae%e7%9a%84%e7%b1%bb%e5%9e%8b%e6%8f%90%e7%a4%ba%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49561\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Type \u4e3a Python \u7c7b\u63d0\u4f9b\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Type \u4e3a Python \u7c7b\u63d0\u4f9b\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\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\u7c73\u4e91\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\u300a\u5982\u4f55\u4f7f\u7528 Type \u4e3a Python \u7c7b\u63d0\u4f9b\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\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\/20241027\/1729998774671dafb60e684.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 Type \u4e3a Python \u7c7b\u63d0\u4f9b\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 Type \u4e3a Python \u7c7b\u63d0\u4f9b\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4e3a python \u7c7b\u63d0\u4f9b\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a<\/strong><\/p>\n<p>\u5728 python \u4e2d\uff0c\u7ed9\u7c7b\u505a\u7c7b\u578b\u63d0\u793a\u65f6\uff0ctype \u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002\u7136\u800c\uff0ctype \u63d0\u793a\u8fc7\u4e8e\u5bbd\u6cdb\uff0c\u4e0d\u8db3\u4ee5\u7cbe\u786e\u63cf\u8ff0\u7c7b\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u5f53\u9762\u5bf9\u9700\u8981\u7cbe\u786e\u7c7b\u578b\u63d0\u793a\u7684\u60c5\u51b5\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 type\uff0c\u5b83\u662f typing \u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u4e00\u4e2a\u7279\u6b8a\u7c7b\u578b\u63d0\u793a\u3002type \u5141\u8bb8\u6307\u5b9a\u4e00\u4e2a\u7279\u5b9a\u7684\u7c7b\u4f5c\u4e3a\u7c7b\u578b\u63d0\u793a\uff0c\u4ece\u800c\u907f\u514d\u4e86 type \u7684\u6cdb\u6cdb\u6027\u3002<\/p>\n<p><strong>\u5e94\u7528\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 type \u4e3a\u7c7b\u63d0\u4f9b\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1a<\/p>\n<pre>from typing import Type\n\nclass MyClass:\n    def __init__(self, name: str, data: dict[str, int | str]) -&gt; None:\n        pass\n\ndef func(cls: Type[MyClass]):\n    pass\n\nfunc(MyClass)<\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cfunc \u51fd\u6570\u7684\u53c2\u6570 cls \u88ab\u660e\u786e\u5730\u6307\u5b9a\u4e3a myclass \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u8fd9\u8868\u793a\u4f20\u5165 func \u7684\u53c2\u6570\u5fc5\u987b\u662f myclass \u7c7b\u7684\u5b9e\u4f8b\u3002\u4f7f\u7528 type \u53ef\u4ee5\u6e05\u695a\u5730\u8868\u793a\u7c7b\u7684\u9884\u671f\u7c7b\u578b\uff0c\u907f\u514d\u4e86\u4f7f\u7528 type \u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u6b67\u4e49\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Type \u4e3a Python \u7c7b\u63d0\u4f9b\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f \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\u7c73\u4e91\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\u300a\u5982\u4f55\u4f7f\u7528 Type \u4e3a Python \u7c7b\u63d0\u4f9b\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\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 \u4e3a python \u7c7b\u63d0\u4f9b\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a \u5728 python \u4e2d\uff0c\u7ed9\u7c7b\u505a\u7c7b\u578b\u63d0\u793a\u65f6\uff0ctype \u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002\u7136\u800c\uff0ctype \u63d0\u793a\u8fc7\u4e8e\u5bbd\u6cdb\uff0c\u4e0d\u8db3\u4ee5\u7cbe\u786e\u63cf\u8ff0\u7c7b\u7684\u7c7b\u578b\u3002 \u5f53\u9762\u5bf9\u9700\u8981\u7cbe\u786e\u7c7b\u578b\u63d0\u793a\u7684\u60c5\u51b5\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 type\uff0c\u5b83\u662f typing \u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u4e00\u4e2a\u7279\u6b8a\u7c7b\u578b\u63d0\u793a\u3002type \u5141\u8bb8\u6307\u5b9a\u4e00\u4e2a\u7279\u5b9a\u7684\u7c7b\u4f5c\u4e3a\u7c7b\u578b\u63d0\u793a\uff0c\u4ece\u800c\u907f\u514d\u4e86 type \u7684\u6cdb\u6cdb\u6027\u3002 \u5e94\u7528\u793a\u4f8b \u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 type \u4e3a\u7c7b\u63d0\u4f9b\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1a from typing import Type class MyClass: def __init__(self, name: str, data: dict[str, int | str]) -&gt; None: pass def func(cls: Type[MyClass]): pass func(MyClass) \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cfunc \u51fd\u6570\u7684\u53c2\u6570 cls [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-49561","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=49561"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49561\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}