{"id":8173,"date":"2022-09-22T10:44:21","date_gmt":"2022-09-22T02:44:21","guid":{"rendered":"http:\/\/139.9.1.231\/?p=8173"},"modified":"2022-09-22T11:07:23","modified_gmt":"2022-09-22T03:07:23","slug":"python-type","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/09\/22\/python-type\/","title":{"rendered":"python  \u7c7b\u578b\u6ce8\u91ca # type"},"content":{"rendered":"\n<h2 id=\"type-comments\u7c7b\u578b\u6ce8\u89e3\">Type Comments[\u7c7b\u578b\u6ce8\u89e3]<\/h2>\n\n\n\n<p>   \u6ce8\u91ca\u662f\u5728Python 3\u4e2d\u5f15\u5165\u7684\uff0c\u5e76\u4e14\u5b83\u4eec\u6ca1\u6709\u88ab\u53cd\u5411\u79fb\u690d\u5230Python 2.\u8fd9\u610f\u5473\u7740\u5982\u679c\u60a8\u6b63\u5728\u7f16\u5199\u9700\u8981\u652f\u6301\u65e7\u7248Python\u7684\u4ee3\u7801\uff0c\u5219\u65e0\u6cd5\u4f7f\u7528\u6ce8\u91ca\u3002<\/p>\n\n\n\n<p>\u8981\u5411\u51fd\u6570\u6dfb\u52a0\u7c7b\u578b\u6ce8\u91ca\uff0c\u60a8\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import math \ndef circumference(radius):    \n<em># type: (float) -> float<\/em>    \n   return 2 * math.pi * radius<\/code><\/pre>\n\n\n\n<p>\u7c7b\u578b\u6ce8\u91ca\u53ea\u662f\u6ce8\u91ca\uff0c\u6240\u4ee5\u5b83\u4eec\u53ef\u4ee5\u7528\u5728\u4efb\u4f55\u7248\u672c\u7684Python\u4e2d\u3002<\/p>\n\n\n\n<p>\u7c7b\u578b\u6ce8\u91ca\u7531\u7c7b\u578b\u68c0\u67e5\u5668\u76f4\u63a5\u5904\u7406\uff0c\u6240\u4ee5\u4e0d\u5b58\u5728__annotations__\u5b57\u5178\u5bf9\u8c61\u4e2d:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; circumference.<strong>__annotations__<\/strong>{}<\/code><\/pre>\n\n\n\n<p><strong>\u7c7b\u578b\u6ce8\u91ca\u5fc5\u987b\u4ee5type: \u5b57\u9762\u91cf\u5f00\u5934\uff0c\u5e76\u4e0e\u51fd\u6570\u5b9a\u4e49\u4f4d\u4e8e\u540c\u4e00\u884c\u6216\u4e0b\u4e00\u884c\u3002\u5982\u679c\u60a8\u60f3\u7528\u51e0\u4e2a\u53c2\u6570\u6765\u6ce8\u91ca\u4e00\u4e2a\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u7528\u9017\u53f7\u5206\u9694\u6bcf\u4e2a\u7c7b\u578b:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def headline(text, width=80, fill_char=\"-\"):  \n  <em># type: (str, int, str) -> str<\/em>    \n   return f\" {text.title()} \".center(width, fill_char) \n\nprint(headline(\"type comments work\", width=40))<\/code><\/pre>\n\n\n\n<p>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u81ea\u5df1\u7684\u6ce8\u91ca\u5728\u5355\u72ec\u7684\u884c\u4e0a\u7f16\u5199\u6bcf\u4e2a\u53c2\u6570:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># headlines.py\r\n \r\n  def headline(\r\n      text,           # type: str\r\n      width=80,       # type: int\r\n      fill_char=\"-\",  # type: str\r\n  ):                  # type: (...) -> str\r\n      return f\" {text.title()} \".center(width, fill_char)\r\n \r\n print(headline(\"type comments work\", width=40))<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7Python\u548cMypy\u8fd0\u884c\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$  python headlines.py\n---------- Type Comments Work ---------- \n$ mypy headline.py\n$<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4f20\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32width=&#8221;full&#8221;\uff0c\u518d\u6b21\u8fd0\u884cmypy\u4f1a\u51fa\u73b0\u4e00\u4e0b\u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mypy headline.py\r\nheadline.py:10: error: Argument \"width\" to \"headline\" has incompatible\r\n                       type \"str\"; expected \"int\"<\/code><\/pre>\n\n\n\n<p>\u60a8\u8fd8\u53ef\u4ee5\u5411\u53d8\u91cf\u6dfb\u52a0\u7c7b\u578b\u6ce8\u91ca\u3002\u8fd9\u4e0e\u60a8\u5411\u53c2\u6570\u6dfb\u52a0\u7c7b\u578b\u6ce8\u91ca\u7684\u65b9\u5f0f\u7c7b\u4f3c:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pi = 3.142  <em># type: float<\/em>\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u68c0\u6d4b\u51fapi\u662ffloat\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Type Comments[\u7c7b\u578b\u6ce8\u89e3] \u6ce8\u91ca\u662f\u5728Python 3\u4e2d\u5f15\u5165\u7684\uff0c\u5e76\u4e14\u5b83\u4eec\u6ca1\u6709\u88ab\u53cd\u5411\u79fb\u690d\u5230Python &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/09\/22\/python-type\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">python  \u7c7b\u578b\u6ce8\u91ca # type<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,11],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/8173"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=8173"}],"version-history":[{"count":18,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/8173\/revisions"}],"predecessor-version":[{"id":8193,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/8173\/revisions\/8193"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=8173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=8173"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=8173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}