itertools.islice \u652f\u6301 index<\/strong><\/div>\nPython \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>\nfunctools.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>\nPython 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}]}}