{"id":204184,"date":"2025-05-29T12:08:28","date_gmt":"2025-05-29T04:08:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204184\/"},"modified":"2025-05-29T12:08:28","modified_gmt":"2025-05-29T04:08:28","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-python-%e4%b8%ad%e8%8e%b7%e5%8f%96-uniontype-%e7%9a%84%e5%ad%90%e6%88%90%e5%91%98%e5%92%8c%e5%88%a4%e6%96%ad%e6%8c%87%e5%ae%9a%e7%b1%bb%e5%9e%8b%e6%98%af%e5%90%a6%e5%9c%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204184\/","title":{"rendered":"\u5982\u4f55\u5728 Python \u4e2d\u83b7\u53d6 UnionType \u7684\u5b50\u6210\u5458\u548c\u5224\u65ad\u6307\u5b9a\u7c7b\u578b\u662f\u5426\u5728 UnionType \u4e2d\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728 Python \u4e2d\u83b7\u53d6 UnionType \u7684\u5b50\u6210\u5458\u548c\u5224\u65ad\u6307\u5b9a\u7c7b\u578b\u662f\u5426\u5728 UnionType \u4e2d\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241031\/17303442056722f50d88666.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u4e2d\u83b7\u53d6 uniontype \u7684\u5b50\u6210\u5458<\/strong><\/p>\n<p>uniontype \u53ef\u4ee5\u8868\u793a\u7c7b\u578b\u7684\u8054\u5408\uff0c\u5728 python \u4e2d\uff0c\u53ef\u4ee5\u5229\u7528 uniontype \u6765\u6307\u5b9a\u4e00\u4e2a\u53c2\u6570\u53ef\u4ee5\u63a5\u6536\u591a\u79cd\u7c7b\u578b\u3002\u4f46\u662f\uff0c\u5982\u4f55\u83b7\u53d6 uniontype \u7684\u5b50\u6210\u5458\u5462\uff1f\u8be5\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u6307\u5b9a\u7c7b\u578b\u662f\u5426\u5728 uniontype \u4e2d\uff1f<\/p>\n<p><strong>\u83b7\u53d6 uniontype \u7684\u5b50\u6210\u5458<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 typing.get_args \u51fd\u6570\uff0c\u53ef\u4ee5\u53d6\u5f97 uniontype \u7684\u201c\u53c2\u6570\u201d\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>from typing import union\nfrom inspect import signature\n\ndef check_func_args_hints(func):\n    typed_signature = signature(func).parameters.items()  # \u83b7\u53d6\u51fd\u6570\u7684\u53c2\u6570\n    for _, parameter in typed_signature:  # \u904d\u5386\u6240\u6709\u53c2\u6570\n        if isinstance(parameter.annotation, union):  # \u68c0\u67e5\u53c2\u6570\u7c7b\u578b\u662f\u5426\u4e3a uniontype\n            return typing.get_args(parameter.annotation)  # \u8fd4\u56de uniontype \u7684\u5b50\u6210\u5458<\/pre>\n<ul>\n<li>\u8c03\u7528\u51fd\u6570\uff1a<\/li>\n<\/ul>\n<pre>def get_score(name: str | none = none) -&gt; float | none:  # \u5b9a\u4e49\u4e00\u4e2a\u5305\u542b uniontype \u7684\u51fd\u6570\n    pass\n\nresult = check_func_args_hints(get_score)\nprint(result)  # \u8f93\u51fa\uff1a('str', nonetype)<\/pre>\n<p><strong>\u5224\u65ad\u6307\u5b9a\u7c7b\u578b\u662f\u5426\u5728 uniontype \u4e2d<\/strong><\/p>\n<ul>\n<li>\u76f4\u63a5\u4f7f\u7528 isinstance \u51fd\u6570\uff0c\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5c5e\u4e8e\u8be5\u7c7b\u578b\uff1a<\/li>\n<\/ul>\n<pre>is_str_in_union = isinstance('', parameter.annotation)  # true \u5982\u679c parameter.annotation \u5305\u542b str<\/pre>\n<ul>\n<li>\u4f7f\u7528 in \u8fd0\u7b97\u7b26\uff0c\u4f46\u524d\u63d0\u662f uniontype \u4e2d\u53ea\u6709\u4e00\u4e2a\u5b50\u7c7b\u578b\uff1a<\/li>\n<\/ul>\n<pre>if type(parameter.annotation) != Union or str in parameter.annotation:\n    is_str_in_union = True<\/pre>\n<p>\u4ee5\u4e0a\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u83b7\u53d6 uniontype \u7684\u5b50\u6210\u5458\u548c\u5224\u65ad\u6307\u5b9a\u7c7b\u578b\u662f\u5426\u5728 uniontype \u4e2d\uff0c\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\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\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 Python \u4e2d\u83b7\u53d6 U&#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-204184","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204184","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=204184"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204184\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}