{"id":302,"date":"2014-04-15T02:52:04","date_gmt":"2014-04-14T17:52:04","guid":{"rendered":"http:\/\/blog.ymyzk.com\/?p=302"},"modified":"2014-12-03T09:57:08","modified_gmt":"2014-12-03T00:57:08","slug":"python-2-3-future-builtins","status":"publish","type":"post","link":"https:\/\/blog.ymyzk.com\/2014\/04\/python-2-3-future-builtins\/","title":{"rendered":"Python 2\/3 \u4e21\u5bfe\u5fdc\u306e\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u305f\u3081\u306e future_builtins \u306e\u4f7f\u3044\u65b9"},"content":{"rendered":"
Python 2 \u3068 Python 3 \u3067\u52d5\u4f5c\u304c\u5909\u308f\u3063\u305f\u7d44\u307f\u8fbc\u307f\u95a2\u6570\u306b, filter \u3084 map \u304c\u3042\u308b. Python 2 \u3067\u3082 Python 3 \u3068\u540c\u3058\u52d5\u4f5c\u3092\u3055\u305b\u308b\u305f\u3081\u306b, future_builtins \u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u308b\u304c, Python 2\/3 \u4e21\u5bfe\u5fdc\u3059\u308b\u306b\u306f\u5c11\u3057\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3042\u308b. \u4ed6\u306e\u3044\u304f\u3064\u304b\u306e\u65b9\u6cd5\u3082\u542b\u3081\u3066, Python 3 \u306e\u4ed5\u69d8\u3067\u52d5\u4f5c\u3059\u308b filter \u3084 map \u95a2\u6570\u306e Python 2\/3 \u4e21\u5bfe\u5fdc\u306e\u66f8\u304d\u65b9\u306b\u3064\u3044\u3066\u7d39\u4ecb\u3059\u308b.<\/p>\n
<\/p>\n
Python 2 \u3067\u306e filter \u95a2\u6570\u3084 map \u95a2\u6570\u306f\u30ea\u30b9\u30c8\u3092\u8fd4\u3057\u3066\u3044\u305f\u304c, Python 3 \u3067\u306f\u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u8fd4\u3059\u3088\u3046\u306b\u306a\u3063\u305f. Python 2 \u306e\u30b3\u30fc\u30c9\u3092 Python 3 \u306e\u30b3\u30fc\u30c9\u306b\u5909\u63db\u3059\u308b 2to3 \u3067\u306f, Python 2 \u306e<\/p>\n
\r\nmap(lambda x: x * 2, [1, 2, 3])\r\n<\/pre>\n\u306e\u30b3\u30fc\u30c9\u3092<\/p>\n
\r\nlist(map(lambda x: x * 2, [1, 2, 3]))\r\n<\/pre>\n\u306b\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u3067\u5bfe\u5fdc\u3057\u3066\u3044\u308b. \u4e0b\u306e\u30b3\u30fc\u30c9\u306f Python 2 \u3067\u3082\u6b63\u5e38\u306b\u3059\u308b\u305f\u3081, Python 2\/3 \u4e21\u5bfe\u5fdc\u306e\u30b3\u30fc\u30c9\u3068\u3057\u3066\u4f7f\u3048\u308b. \u3057\u304b\u3057, map \u304c\u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u8fd4\u3059\u3068\u3044\u3046\u4ed5\u69d8\u3092\u3046\u307e\u304f\u5229\u7528\u3059\u308b\u3053\u3068\u306f\u51fa\u6765\u306a\u3044.<\/p>\n
Python 2 \u3067\u306f\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306e itertools \u306b ifilter \u3084 imap \u3068\u3044\u3063\u305f\u95a2\u6570\u304c\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u3066, \u3053\u308c\u3089\u306f\u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u8fd4\u3057, \u305d\u308c\u305e\u308c Python 3 \u306e filter, map \u306b\u5bfe\u5fdc\u3059\u308b. \u3057\u304b\u3057\u306a\u304c\u3089 Python 3 \u306e itertools \u306b\u306f\u3053\u308c\u3089\u306e\u95a2\u6570\u304c\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u306a\u3044\u305f\u3081, Python 2\/3 \u4e21\u5bfe\u5fdc\u306e\u30b3\u30fc\u30c9\u306b\u306f\u4f7f\u3044\u3065\u3089\u3044.<\/p>\n
Python 2 \u306e\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u3068\u3057\u3066\u63d0\u4f9b\u3055\u308c\u3066\u3044\u308b future_builtins \u3092\u4f7f\u3046\u3068, Python 3 \u306e\u3088\u3046\u306b\u30a4\u30c6\u30ec\u30fc\u30bf\u3092\u8fd4\u3059 filter \u3084 map \u3092\u4f7f\u3046\u3053\u3068\u304c\u51fa\u6765\u308b. \u4f7f\u3044\u65b9\u306f\u901a\u5e38\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u8aad\u307f\u8fbc\u3080\u3068\u304d\u3068\u540c\u3058\u3067, \u4ee5\u4e0b\u306b\u3088\u3046\u306b\u3059\u308b.<\/p>\n
\r\nfrom future_builtins import filter, map\r\n<\/pre>\nPython 3 \u3067\u306f future_builtins \u306f\u63d0\u4f9b\u3055\u308c\u3066\u3044\u306a\u3044\u306e\u3067, \u3053\u306e\u307e\u307e\u3067\u306f Python 2\/3 \u4e21\u5bfe\u5fdc\u306e\u30b3\u30fc\u30c9\u306b\u306f\u306a\u3089\u306a\u3044. \u4ee5\u4e0b\u306e\u3088\u3046\u306b try \u6587\u3092\u7528\u3044\u3066, Python 3 \u3067 future_builtins \u3092\u8aad\u307f\u8fbc\u3081\u306a\u304b\u3063\u305f\u5834\u5408\u306e\u30a8\u30e9\u30fc\u51e6\u7406\u3092\u884c\u306a\u3046\u3068, Python 2\/3 \u4e21\u5bfe\u5fdc\u306e\u30b3\u30fc\u30c9\u306b\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u308b.<\/p>\n
\r\ntry:\r\n from future_builtins import filter, map\r\nexcept ImportError:\r\n pass\r\n<\/pre>\nprint \u95a2\u6570\u3092 Python 2 \u3067\u3082\u7528\u3044\u308b, __future__.print_function \u306f Python 3 \u3067 import \u3057\u3066\u3082\u30a8\u30e9\u30fc\u306b\u306a\u3089\u306a\u3044\u304c, future_builtins \u3092 Python 3 \u3067\u8aad\u307f\u8fbc\u3082\u3046\u3068\u3059\u308b\u3068\u30a8\u30e9\u30fc\u304c\u767a\u58f0\u3059\u308b\u306e\u3067\u8981\u6ce8\u610f\u3067\u3042\u308b.<\/p>\n","protected":false},"excerpt":{"rendered":"
Python 2 \u3068 Python 3 \u3067\u52d5\u4f5c\u304c\u5909\u308f\u3063\u305f\u7d44\u307f\u8fbc\u307f\u95a2\u6570\u306b, filter \u3084 map \u304c\u3042\u308b. Python 2 \u3067\u3082 Python 3 \u3068\u540c\u3058\u52d5\u4f5c\u3092\u3055\u305b\u308b\u305f\u3081\u306b, future_builtins \u3068\u3044\u3046\u30e9 […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[15],"_links":{"self":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/302"}],"collection":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/comments?post=302"}],"version-history":[{"count":7,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/302\/revisions"}],"predecessor-version":[{"id":643,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/302\/revisions\/643"}],"wp:attachment":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/media?parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/categories?post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/tags?post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}