{"id":220220,"date":"2021-06-20T08:23:31","date_gmt":"2021-06-20T00:23:31","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=220220"},"modified":"2021-06-08T09:24:34","modified_gmt":"2021-06-08T01:24:34","slug":"python-3-infinite-generator","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-3-infinite-generator.html","title":{"rendered":"Python 3.7\u7279\u6027\u5207\u7247\u65e0\u9650\u751f\u6210\u5668"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u8fd9\u4e2a\u548c\u5176\u4ed6\u4e24\u4e2a\u672a\u88ab\u5145\u5206\u5229\u7528\u4f46\u4ecd\u7136\u6709\u7528\u7684 Python \u7279\u6027\u3002\u8fd9\u662f\u5173\u4e8e Python 3.x \u9996\u53d1\u7279\u6027\u7cfb\u5217\u6587\u7ae0\u7684\u7b2c\u516b\u7bc7\u3002Python 3.7 \u4e8e 2018 \u5e74\u9996\u6b21\u53d1\u5e03\uff0c\u5c3d\u7ba1\u5b83\u5df2\u7ecf\u53d1\u5e03\u4e86\u51e0\u5e74\uff0c\u4f46\u5b83\u5f15\u5165\u7684\u8bb8\u591a\u7279\u6027\u90fd\u672a\u88ab\u5145\u5206\u5229\u7528\uff0c\u800c\u4e14\u76f8\u5f53\u9177\u3002\u4e0b\u9762\u662f\u5176\u4e2d\u7684\u4e09\u4e2a\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u6ce8\u89e3\u63a8\u8fdf\u8bc4\u4f30<\/strong><\/div>\n

\u5728 Python 3.7 \u4e2d\uff0c\u53ea\u8981\u6fc0\u6d3b\u4e86\u6b63\u786e\u7684 __future__ \u6807\u5fd7\uff0c\u6ce8\u89e3\u5728\u8fd0\u884c\u65f6\u5c31\u4e0d\u4f1a\u88ab\u8bc4\u4f30\uff1a<\/p>\n

\r\nfrom __future__ import annotations\r\n \r\ndef another_brick(wall: List[Brick], brick: Brick) -> Education:\r\n    pass\r\n<\/pre>\n
\r\nanother_brick.__annotations__\r\n<\/pre>\n
\r\n    {'wall': 'List[Brick]', 'brick': 'Brick', 'return': 'Education'}\r\n<\/pre>\n

\u5b83\u4f7f\u9012\u5f52\u7c7b\u578b\uff08\u6307\u5411\u81ea\u5df1\u7684\u7c7b\uff09\u548c\u5176\u4ed6\u6709\u8da3\u7684\u4e8b\u60c5\u6210\u4e3a\u4e86\u53ef\u80fd\u3002\u7136\u800c\uff0c\u8fd9\u610f\u5473\u7740\u5982\u679c\u4f60\u60f3\u505a\u81ea\u5df1\u7684\u7c7b\u578b\u5206\u6790\uff0c\u4f60\u9700\u8981\u660e\u786e\u5730\u4f7f\u7528 ast\u3002<\/p>\n

\r\nimport ast\r\nraw_type = another_brick.__annotations__['wall']\r\n[parsed_type] = ast.parse(raw_type).body\r\n<\/pre>\n
\r\nsubscript = parsed_type.value\r\nf\"{subscript.value.id}[{subscript.slice.id}]\"\r\n<\/pre>\n
\r\n    'List[Brick]'\r\n<\/pre>\n
itertools.islice \u652f\u6301 index<\/strong><\/div>\n

Python \u4e2d\u7684\u5e8f\u5217\u5207\u7247\u957f\u671f\u4ee5\u6765\u4e00\u76f4\u63a5\u53d7\u5404\u79cd \u7c7b int \u5bf9\u8c61\uff08\u5177\u6709 __index__() \u7684\u5bf9\u8c61\uff09\u4f5c\u4e3a\u6709\u6548\u7684\u5207\u7247\u90e8\u5206\u3002\u7136\u800c\uff0c\u76f4\u5230 Python 3.7\uff0citertools.islice\uff0c\u5373\u6838\u5fc3 Python \u4e2d\u5bf9\u65e0\u9650\u751f\u6210\u5668\u8fdb\u884c\u5207\u7247\u7684\u552f\u4e00\u65b9\u6cd5\uff0c\u624d\u83b7\u5f97\u4e86\u8fd9\u79cd\u652f\u6301\u3002<\/p>\n

\u4f8b\u5982\uff0c\u73b0\u5728\u53ef\u4ee5\u7528 numpy.short \u5927\u5c0f\u7684\u6574\u6570\u6765\u5207\u7247\u65e0\u9650\u751f\u6210\u5668\uff1a<\/p>\n

\r\nimport numpy\r\nshort_1 = numpy.short(1)\r\nshort_3 = numpy.short(3)\r\nshort_1, type(short_1)\r\n<\/pre>\n
\r\n    (1, numpy.int16)\r\n<\/pre>\n
\r\nimport itertools\r\nlist(itertools.islice(itertools.count(), short_1, short_3))\r\n<\/pre>\n
\r\n    [1, 2]\r\n<\/pre>\n
functools.singledispatch() \u6ce8\u89e3\u6ce8\u518c<\/strong><\/div>\n

\u5982\u679c\u4f60\u8ba4\u4e3a singledispatch \u5df2\u7ecf\u5f88\u9177\u4e86\uff0c\u4f60\u9519\u4e86\u3002\u73b0\u5728\u53ef\u4ee5\u6839\u636e\u6ce8\u89e3\u6765\u6ce8\u518c\u4e86\uff1a<\/p>\n

\r\nimport attr\r\nimport math\r\nfrom functools import singledispatch\r\n \r\n@attr.s(auto_attribs=True, frozen=True)\r\nclass Circle:\r\n    radius: float\r\n       \r\n@attr.s(auto_attribs=True, frozen=True)\r\nclass Square:\r\n    side: float\r\n \r\n@singledispatch\r\ndef get_area(shape):\r\n    raise NotImplementedError(\"cannot calculate area for unknown shape\",\r\n                              shape)\r\n \r\n@get_area.register\r\ndef _get_area_square(shape: Square):\r\n    return shape.side ** 2\r\n \r\n@get_area.register\r\ndef _get_area_circle(shape: Circle):\r\n    return math.pi * (shape.radius ** 2)\r\n \r\nget_area(Circle(1)), get_area(Square(1))\r\n<\/pre>\n
\r\n    (3.141592653589793, 1)\r\n<\/pre>\n
\u6b22\u8fce\u6765\u5230 2017 \u5e74<\/strong><\/div>\n

Python 3.7 \u5927\u7ea6\u662f\u56db\u5e74\u524d\u53d1\u5e03\u7684\uff0c\u4f46\u662f\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u4e00\u4e9b\u7279\u6027\u975e\u5e38\u9177\uff0c\u800c\u4e14\u6ca1\u6709\u5f97\u5230\u5145\u5206\u5229\u7528\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u4f7f\u7528\uff0c\u90a3\u4e48\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230\u4f60\u7684\u5de5\u5177\u7bb1\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728 Python 3.7 \u4e2d\uff0c\u53ea\u8981\u6fc0\u6d3b\u4e86\u6b63\u786e\u7684 __future__ \u6807\u5fd7\uff0c\u6ce8\u89e3\u5728\u8fd0\u884c\u65f6\u5c31\u4e0d\u4f1a\u88ab\u8bc4\u4f30\uff1a fro […]<\/p>\n","protected":false},"author":1329,"featured_media":220222,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-220220","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=220220"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220220\/revisions"}],"predecessor-version":[{"id":220225,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220220\/revisions\/220225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/220222"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=220220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=220220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=220220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}