{"id":2114,"date":"2024-11-16T16:02:33","date_gmt":"2024-11-16T08:02:33","guid":{"rendered":"http:\/\/blog.xtaa.cn\/?p=2114"},"modified":"2024-11-16T16:02:33","modified_gmt":"2024-11-16T08:02:33","slug":"python-annotated-%e6%b3%a8%e9%87%8a%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"http:\/\/blog.xtaa.cn\/index.php\/2024\/11\/16\/python-annotated-%e6%b3%a8%e9%87%8a%e6%a8%a1%e5%9d%97\/","title":{"rendered":"python Annotated \u6ce8\u91ca\u6a21\u5757"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662fAnnotated\u6a21\u5757\uff1f<\/h2>\n\n\n\n<p>Annotated\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u6ce8\u89e3\uff08Annotation\uff09\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u6ce8\u89e3\u662fPython 3.0\u5f15\u5165\u7684\u4e00\u79cd\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u5728\u51fd\u6570\u3001\u7c7b\u548c\u65b9\u6cd5\u7684\u5b9a\u4e49\u4e2d\u6dfb\u52a0\u989d\u5916\u7684\u4fe1\u606f\uff0c\u8fd9\u4e9b\u4fe1\u606f\u53ef\u4ee5\u7528\u4e8e\u7c7b\u578b\u68c0\u67e5\u3001\u6587\u6863\u751f\u6210\u7b49\u7528\u9014\u3002Annotated\u6a21\u5757\u901a\u8fc7\u63d0\u4f9b\u4e00\u4e9b\u88c5\u9970\u5668\u548c\u5de5\u5177\u51fd\u6570\uff0c\u4f7f\u5f97\u6ce8\u89e3\u7684\u4f7f\u7528\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u548c\u4fbf\u6377\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Annotated\u6a21\u5757\u7684\u4f7f\u7528\u65b9\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b89\u88c5Annotated\u6a21\u5757<\/h3>\n\n\n\n<p>\u5728\u4f7f\u7528Annotated\u6a21\u5757\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u7528pip\u5b89\u88c5Annotated\u6a21\u5757\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install Annotated\n<\/code><\/pre>\n\n\n\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5c31\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165Annotated\u6a21\u5757\u4e86\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u672c\u6ce8\u89e3\u7c7b\u578b<\/h3>\n\n\n\n<p>Annotated\u6a21\u5757\u63d0\u4f9b\u4e86\u51e0\u79cd\u57fa\u672c\u7684\u6ce8\u89e3\u7c7b\u578b\uff0c\u5305\u62ec<code>Annotated<\/code>\u3001<code>Union<\/code>\u548c<code>Optional<\/code>\u3002\u8fd9\u4e9b\u7c7b\u578b\u53ef\u4ee5\u7528\u4e8e\u5bf9\u51fd\u6570\u53c2\u6570\u3001\u8fd4\u56de\u503c\u548c\u53d8\u91cf\u8fdb\u884c\u6ce8\u89e3\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Annotated[type, metadata]<\/code>\uff1a\u7528\u4e8e\u6ce8\u89e3\u5bf9\u8c61\u7684\u7c7b\u578b\u5e76\u6dfb\u52a0\u5143\u6570\u636e\u3002<\/li>\n\n\n\n<li><code>Union[type1, type2, ...]<\/code>\uff1a\u8868\u793a\u6ce8\u89e3\u5bf9\u8c61\u7684\u7c7b\u578b\u53ef\u4ee5\u662f\u591a\u4e2a\u7c7b\u578b\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u3002<\/li>\n\n\n\n<li><code>Optional[type]<\/code>\uff1a\u8868\u793a\u6ce8\u89e3\u5bf9\u8c61\u7684\u7c7b\u578b\u53ef\u4ee5\u662f\u6307\u5b9a\u7c7b\u578b\u6216\u8005<code>None<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u57fa\u672c\u6ce8\u89e3\u7c7b\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from Annotated import Annotated, Union, Optional\n\ndef greet(name: Annotated&#91;str, \"The name of the person\"]) -> str:\n    return \"Hello, \" + name\n\ndef add(a: int, b: int) -> int:\n    return a + b\n\ndef divide(a: int, b: Annotated&#91;int, \"The divisor\"], *, remainder: Optional&#91;bool] = False) -> Union&#91;int, float]:\n    if remainder:\n        return a % b\n    else:\n        return a \/ b<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>greet<\/code>\u51fd\u6570\u7684\u53c2\u6570<code>name<\/code>\u88ab\u6ce8\u89e3\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u5e76\u6dfb\u52a0\u4e86\u4e00\u4e2a\u63cf\u8ff0\u4fe1\u606f\u3002<code>add<\/code>\u51fd\u6570\u7684\u53c2\u6570<code>a<\/code>\u548c<code>b<\/code>\u88ab\u6ce8\u89e3\u4e3a\u6574\u6570\u7c7b\u578b\uff0c\u8fd4\u56de\u503c\u88ab\u6ce8\u89e3\u4e3a\u6574\u6570\u7c7b\u578b\u3002<code>divide<\/code>\u51fd\u6570\u7684\u53c2\u6570<code>a<\/code>\u548c<code>b<\/code>\u88ab\u6ce8\u89e3\u4e3a\u6574\u6570\u7c7b\u578b\uff0c\u5e76\u6dfb\u52a0\u4e86\u63cf\u8ff0\u4fe1\u606f\uff0c<code>remainder<\/code>\u53c2\u6570\u88ab\u6ce8\u89e3\u4e3a\u5e03\u5c14\u7c7b\u578b\uff0c\u9ed8\u8ba4\u503c\u4e3a<code>False<\/code>\uff0c\u8fd4\u56de\u503c\u53ef\u4ee5\u662f\u6574\u6570\u6216\u8005\u6d6e\u70b9\u6570\u7c7b\u578b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u6ce8\u89e3<\/h3>\n\n\n\n<p>\u5728\u4f7f\u7528Annotated\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>Annotated<\/code>\u3001<code>Union<\/code>\u548c<code>Optional<\/code>\u7b49\u7c7b\u578b\u6765\u6dfb\u52a0\u6ce8\u89e3\u3002\u6ce8\u89e3\u53ef\u4ee5\u7528\u4e8e\u51fd\u6570\u7684\u53c2\u6570\u3001\u8fd4\u56de\u503c\u548c\u53d8\u91cf\u7684\u58f0\u660e\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from typing import Annotated, Union, Optional\n\ndef greet(name: Annotated&#91;str, \"The name of the person\"]) -&gt; str:\n    return \"Hello, \" + name\n\ndef divide(a: int, b: Annotated&#91;int, \"The divisor\"], *, remainder: Optional&#91;bool] = False) -&gt; Union&#91;int, float]:\n    if remainder:\n        return a % b\n    else:\n        return a \/ b\n\nresult = greet(\"Alice\")\nprint(result)  <em># \u8f93\u51fa\uff1aHello, Alice<\/em>\n\nresult = divide(10, 3)\nprint(result)  <em># \u8f93\u51fa\uff1a3.3333333333333335<\/em>\n\nresult = divide(10, 3, remainder=True)\nprint(result)  <em># \u8f93\u51fa\uff1a1<\/em>\n<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u8c03\u7528<code>Annotated<\/code>\u51fd\u6570\u6765\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u6dfb\u52a0\u6ce8\u89e3\uff0c\u6ce8\u89e3\u7684\u7c7b\u578b\u53ef\u4ee5\u662f\u57fa\u672c\u7c7b\u578b\uff0c\u4e5f\u53ef\u4ee5\u662f\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002\u5728\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4f20\u5165\u7b26\u5408\u6ce8\u89e3\u7c7b\u578b\u7684\u5b9e\u53c2\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u83b7\u53d6\u6ce8\u89e3\u4fe1\u606f<\/h3>\n\n\n\n<p>\u4f7f\u7528Annotated\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u51fd\u6570\u3001\u65b9\u6cd5\u548c\u7c7b\u7684\u6ce8\u89e3\u4fe1\u606f\u3002\u53ef\u4ee5\u901a\u8fc7<code>get_type_hints<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u51fd\u6570\u4e2d\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u6ce8\u89e3\u4fe1\u606f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from Annotated import get_type_hints\n\ndef greet(name: Annotated&#91;str, \"The name of the person\"]) -&gt; str:\n    return \"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fAnnotated\u6a21\u5757\uff1f Annotated\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u6ce8\u89e3\uff08Anno [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-2114","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/2114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2114"}],"version-history":[{"count":1,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/2114\/revisions"}],"predecessor-version":[{"id":2115,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/2114\/revisions\/2115"}],"wp:attachment":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2114"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}