{"id":8929,"date":"2023-09-13T01:35:32","date_gmt":"2023-09-12T17:35:32","guid":{"rendered":"\/?p=8929"},"modified":"2023-09-13T01:36:48","modified_gmt":"2023-09-12T17:36:48","slug":"fastapi%e5%9f%ba%e7%a1%80%ef%bc%9a1-%e7%b1%bb%e5%9e%8b%e6%a0%87%e8%af%86","status":"publish","type":"post","link":"\/?p=8929","title":{"rendered":"FastAPI\u57fa\u7840\uff1a1. \u7c7b\u578b\u6807\u8bc6"},"content":{"rendered":"<p>ps\uff1aPython3.5 \u5f15\u5165\uff0c\u63d0\u4f9b\u7f16\u5199\u4ee3\u7801\u65f6\u7684\u89c4\u8303\uff0c\u65b9\u6cd5\u8054\u60f3 <a href=\"https:\/\/docs.python.org\/zh-cn\/3.5\/library\/typing.html\">26.1. typing \u2014 \u7c7b\u578b\u6807\u6ce8\u652f\u6301 \u2014 Python 3.5.10 \u6587\u6863<\/a><\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/640.png\" alt=\"\u56fe\u7247\" \/><\/p>\n<h2>\u5e38\u7528\u7c7b\u578b\u6807\u8bc6<\/h2>\n<h3>\u57fa\u7840\u6570\u636e<\/h3>\n<ul>\n<li>str - \u5b57\u7b26\u4e32<\/li>\n<li>int - \u6570\u5b57<\/li>\n<li>bool - \u5e03\u5c14<\/li>\n<li>Any - \u4efb\u610f\u7c7b\u578b<\/li>\n<li>Callback - \u53ef\u8c03\u7528\u7c7b\u578b\uff1b\u57fa\u672c\u7528\u4e8e\u51fd\u6570 Callable[[Arg1Type, Arg2Type], ReturnType]<\/li>\n<li>List[str] - \u5217\u8868\u4e14\u5185\u90e8\u5143\u7d20 \u90fd\u662f\u5b57\u7b26\u4e32\u7c7b\u578b<\/li>\n<li><strong>Optional<\/strong>[str] - \u53ef\u9009\u7c7b\u578b\uff08\u5b57\u7b26\u4e32\uff09 \u9ed8\u8ba4\u4e3a None<\/li>\n<\/ul>\n<pre><code class=\"language-python\">&quot;&quot;&quot;\n1\u3001\u7c7b\u578b\u6807\u8bc6 Python3.5 \u5f15\u5165\n&quot;&quot;&quot;\nfrom typing import *\n\nname = &quot;777&quot;\nname = 1\n\nmy_name: str = &quot;7777&quot;\n\nmy_name = 1  # Expected type &#039;str&#039;, got &#039;int&#039; instead\n\nage: int = 18\n\nis_true: bool = False\n\n# \u5217\u8868 \u91cc\u9762\u5143\u7d20\u9700\u8981 \u662f \u5b57\u7b26\u4e32\nnames: List[str] = [1, 2, &quot;name&quot;]\n\n# \u5e8f\u5217\u7c7b\u578b \\ list\\ str\\ tuple\\ set\nloves: Sequence[str] = [&quot;123&quot;, &quot;123&quot;]\nloves: Sequence[str] = &quot;123&quot;\nloves: Sequence[str] = 1\n\n# \u5b57\u5178 key \u662f \u5b57\u7b26\u4e32\u7c7b\u578b value \u662fint\u7c7b\u578b\ninfo: Dict[str, int] = {&quot;age&quot;: 18}\n\n# Mapping\ninfoss: Mapping[str, int] = {1: 1}\n\n# \u7c7b\u578b\u522b\u540d\ninfoType = Dict[str, Any]\n\npeople: infoType = {&quot;info&quot;: 2}\n\n# \u8054\u5408\u7c7b\u578b \u53ef\u4ee5\u662f\u5e03\u5c14\u3001\u4e5f\u53ef\u4ee5\u662fint\nflag: Union[bool, int] = 1  # True\n\n# \u53ef\u9009\u7c7b\u578b, \u53ef\u4ee5\u4e3anone\uff0c\u4e0d\u4e3aNone \u5c31\u9700\u8981\u662fint\u7c7b\u578b\nage_optional: Optional[int] = None\n\n# -&gt; str \u8fd4\u56de\u7c7b\u578b\ndef func1(a: int) -&gt; str:\n    return str(a)\n\n# \u53ef\u8c03\u7528\u7c7b\u578b\ndef hello(func: Callable[[int], str]):\n    return func(13)\n\nprint(hello(func1))\n\n# \u521b\u5efa\u65b0\u7c7b\u578b\nUserId = NewType(&quot;UserId&quot;, int)\n\nuuid: UserId = 1\n\n# \u6cdb\u578b\uff1a\u8fd0\u884c\u65f6\u6839\u636e\u7528\u6237\u4f20\u5165\u7c7b\u578b\u51b3\u5b9a\u7c7b\u578b\n# \u5b98\u65b9\u5b9a\u4e49\uff1a\u7531\u4e8e\u65e0\u6cd5\u4ee5\u901a\u7528\u65b9\u5f0f\u9759\u6001\u63a8\u65ad\u6709\u5173\u4fdd\u5b58\u5728\u5bb9\u5668\u4e2d\u7684\u5bf9\u8c61\u7684\u7c7b\u578b\u4fe1\u606f\uff0c\u56e0\u6b64\u62bd\u8c61\u57fa\u7c7b\u5df2\u6269\u5c55\u4e3a\u652f\u6301\u8ba2\u9605\u4ee5\u8868\u793a\u5bb9\u5668\u5143\u7d20\u7684\u9884\u671f\u7c7b\u578b\u3002\nT = TypeVar(&quot;T&quot;)\n\n# \u6cdb\u578b\u7c7b\nclass Info(Generic[T]):\n    def __init__(self, value: T):\n        self.info = value\n\nInfo(value=1)\nInfo(value=&quot;333&quot;)\nInfo(value=True)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ps\uff1aPython3.5 \u5f15\u5165\uff0c\u63d0\u4f9b\u7f16\u5199\u4ee3\u7801\u65f6\u7684\u89c4\u8303\uff0c\u65b9\u6cd5\u8054\u60f3 26.1. typing \u2014 \u7c7b\u578b\u6807\u6ce8\u652f\u6301 \u2014 Python 3.5.1   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[204],"tags":[116],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8929"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8929"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8929\/revisions"}],"predecessor-version":[{"id":8930,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8929\/revisions\/8930"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8929"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}