{"id":49612,"date":"2024-12-03T14:48:10","date_gmt":"2024-12-03T06:48:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49612\/"},"modified":"2024-12-03T14:48:10","modified_gmt":"2024-12-03T06:48:10","slug":"python-%e7%b1%bb%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e5%87%86%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\/49612\/","title":{"rendered":"Python \u7c7b\u5982\u4f55\u6dfb\u52a0\u51c6\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u7c7b\u5982\u4f55\u6dfb\u52a0\u51c6\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f<\/h1>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241029\/1730185075672087737ea41.jpg\" class=\"aligncenter\" title=\"Python \u7c7b\u5982\u4f55\u6dfb\u52a0\u51c6\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f\u63d2\u56fe\" alt=\"Python \u7c7b\u5982\u4f55\u6dfb\u52a0\u51c6\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u7ed9 python \u7c7b\u6dfb\u52a0\u51c6\u786e\u7684\u7c7b\u578b\u63d0\u793a<\/strong><\/p>\n<p>\u5728 python \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 type \u4e3a\u7c7b\u6dfb\u52a0\u7c7b\u578b\u63d0\u793a\uff0c\u4f46\u5b83\u8fc7\u4e8e\u5bbd\u6cdb\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u4ee3\u7801\u4e2d myclass \u7c7b\u7684 cls \u53c2\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u7c7b\u578b\uff1a<\/p>\n<pre>from loguru import logger\n\nclass myclass:\n    def __init__(self, name: str, data: dict[str, int | str]) -&gt; none:\n        pass\n\ndef func(cls: type):\n    pass\n\nfunc(myclass)<\/pre>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 typing \u6a21\u5757\u4e2d\u7684 type \u6765\u6307\u5b9a\u4e00\u4e2a\u66f4\u51c6\u786e\u7684\u7c7b\u578b\u63d0\u793a\u3002\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>from typing import Type\n\ndef func(cls: Type[MyClass]):\n    pass<\/pre>\n<p>\u73b0\u5728\uff0ccls \u53c2\u6570\u5c06\u51c6\u786e\u5730\u6307\u660e\u5b83\u5e94\u8be5\u662f\u4e00\u4e2a myclass \u7c7b\u7684\u7c7b\u578b\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62\u5c06\u5176\u4ed6\u7c7b\u578b\u9519\u8bef\u5730\u4f20\u7ed9 func \u51fd\u6570\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>Python \u7c7b\u5982\u4f55\u6dfb\u52a0\u51c6\u786e\u7684\u7c7b\u578b\u63d0\u793a\uff1f \u5982\u4f55\u7ed9 python \u7c7b\u6dfb\u52a0\u51c6\u786e\u7684\u7c7b\u578b\u63d0\u793a \u5728 python \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 type \u4e3a\u7c7b\u6dfb\u52a0\u7c7b\u578b\u63d0\u793a\uff0c\u4f46\u5b83\u8fc7\u4e8e\u5bbd\u6cdb\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u4ee3\u7801\u4e2d myclass \u7c7b\u7684 cls \u53c2\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u7c7b\u578b\uff1a from loguru import logger class myclass: def __init__(self, name: str, data: dict[str, int | str]) -&gt; none: pass def func(cls: type): pass func(myclass) \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 typing \u6a21\u5757\u4e2d\u7684 type \u6765\u6307\u5b9a\u4e00\u4e2a\u66f4\u51c6\u786e\u7684\u7c7b\u578b\u63d0\u793a\u3002\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a from typing import Type def func(cls: Type[MyClass]): pass \u73b0\u5728\uff0ccls \u53c2\u6570\u5c06\u51c6\u786e\u5730\u6307\u660e\u5b83\u5e94\u8be5\u662f\u4e00\u4e2a myclass \u7c7b\u7684\u7c7b\u578b\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62\u5c06\u5176\u4ed6\u7c7b\u578b\u9519\u8bef\u5730\u4f20\u7ed9 func [&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-49612","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49612","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=49612"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49612\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}