{"id":49569,"date":"2024-12-03T08:27:29","date_gmt":"2024-12-03T00:27:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49569\/"},"modified":"2024-12-03T08:27:29","modified_gmt":"2024-12-03T00:27:29","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-type-%e7%b1%bb%e5%9e%8b%e5%88%ab%e5%90%8d%e7%bb%99-python-%e7%b1%bb%e8%ae%be%e7%bd%ae%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\/49569\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Type \u7c7b\u578b\u522b\u540d\u7ed9 Python \u7c7b\u8bbe\u7f6e\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Type \u7c7b\u578b\u522b\u540d\u7ed9 Python \u7c7b\u8bbe\u7f6e\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5982\u4f55\u4f7f\u7528 Type \u7c7b\u578b\u522b\u540d\u7ed9 Python \u7c7b\u8bbe\u7f6e\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730016952671df6b8aae63.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 Type \u7c7b\u578b\u522b\u540d\u7ed9 Python \u7c7b\u8bbe\u7f6e\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 Type \u7c7b\u578b\u522b\u540d\u7ed9 Python \u7c7b\u8bbe\u7f6e\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u7ed9 python \u7c7b\u8bbe\u7f6e\u7cbe\u786e\u7c7b\u578b\u63d0\u793a<\/strong><\/p>\n<p>\u5728\u7f16\u5199 python \u4ee3\u7801\u65f6\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u7c7b\u578b\u63d0\u793a\u6765\u6307\u5b9a\u53d8\u91cf\u548c\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u3002\u5728\u7ed9\u7c7b\u6dfb\u52a0\u7c7b\u578b\u63d0\u793a\u65f6\uff0ctype \u867d\u7136\u53ef\u7528\uff0c\u4f46\u7531\u4e8e\u8fc7\u4e8e\u5bbd\u6cdb\u800c\u9650\u5236\u6027\u4e0d\u591f\u3002\u4e3a\u4e86\u8bbe\u5b9a\u66f4\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 from typing import type \u5bfc\u5165 type \u7c7b\u578b\u522b\u540d\u3002<\/p>\n<p>type \u7c7b\u578b\u522b\u540d\u5141\u8bb8\u4f60\u6307\u5b9a\u7c7b\u7684\u786e\u5207\u7c7b\u578b\uff0c\u4ece\u800c\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 type \u7ed9\u7c7b\u7c7b\u578b\u63d0\u793a\u7684\u793a\u4f8b\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\u6b64\u793a\u4f8b\u4e2d\uff0cfunc \u51fd\u6570\u7684\u53c2\u6570 cls \u88ab\u58f0\u660e\u4e3a type[myclass], \u8fd9\u660e\u786e\u6307\u5b9a\u4e86 cls \u53ef\u4ee5\u63a5\u53d7 myclass \u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u901a\u8fc7\u4f7f\u7528 type \u7c7b\u578b\u522b\u540d\uff0c\u6211\u4eec\u7ed9\u7c7b\u578b\u63d0\u793a\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7cbe\u5ea6\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7406\u89e3\u6027\u548c\u53ef\u7ef4\u62a4\u6027\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\u300a\u5982\u4f55\u4f7f\u7528 Type \u7c7b\u578b\u522b\u540d\u7ed9 Python \u7c7b\u8bbe\u7f6e\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Type \u7c7b\u578b\u522b\u540d\u7ed9 Python \u7c7b\u8bbe\u7f6e\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f \u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5982\u4f55\u4f7f\u7528 Type \u7c7b\u578b\u522b\u540d\u7ed9 Python \u7c7b\u8bbe\u7f6e\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002 \u7ed9 python \u7c7b\u8bbe\u7f6e\u7cbe\u786e\u7c7b\u578b\u63d0\u793a \u5728\u7f16\u5199 python \u4ee3\u7801\u65f6\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u7c7b\u578b\u63d0\u793a\u6765\u6307\u5b9a\u53d8\u91cf\u548c\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u3002\u5728\u7ed9\u7c7b\u6dfb\u52a0\u7c7b\u578b\u63d0\u793a\u65f6\uff0ctype \u867d\u7136\u53ef\u7528\uff0c\u4f46\u7531\u4e8e\u8fc7\u4e8e\u5bbd\u6cdb\u800c\u9650\u5236\u6027\u4e0d\u591f\u3002\u4e3a\u4e86\u8bbe\u5b9a\u66f4\u7cbe\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 from typing import type \u5bfc\u5165 type \u7c7b\u578b\u522b\u540d\u3002 type \u7c7b\u578b\u522b\u540d\u5141\u8bb8\u4f60\u6307\u5b9a\u7c7b\u7684\u786e\u5207\u7c7b\u578b\uff0c\u4ece\u800c\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 type \u7ed9\u7c7b\u7c7b\u578b\u63d0\u793a\u7684\u793a\u4f8b\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\u6b64\u793a\u4f8b\u4e2d\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-49569","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49569","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=49569"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49569\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}