{"id":1294,"date":"2016-02-29T10:54:31","date_gmt":"2016-02-29T01:54:31","guid":{"rendered":"https:\/\/blog.ymyzk.com\/?p=1294"},"modified":"2017-05-31T22:55:01","modified_gmt":"2017-05-31T13:55:01","slug":"python-2-type-hints","status":"publish","type":"post","link":"https:\/\/blog.ymyzk.com\/2016\/02\/python-2-type-hints\/","title":{"rendered":"Python 2 \u3068\u578b\u30d2\u30f3\u30c8 (Type Hints)"},"content":{"rendered":"

\u3053\u306e\u8a18\u4e8b\u3067\u306f PEP 484 — Type Hints<\/a> \u306e\u66f4\u65b0\u306b\u3088\u3063\u3066\u63d0\u6848\u3055\u308c\u3066\u3044\u308b Python 2 \u306b\u304a\u3051\u308b\u578b\u30d2\u30f3\u30c8\u306b\u3064\u3044\u3066\u306e\u60c5\u5831\u3092\u307e\u3068\u3081\u307e\u3059.<\/p>\n

<\/p>\n

\u578b\u30d2\u30f3\u30c8\u3068\u306f<\/h2>\n

Python 3.5 \u304b\u3089\u5c0e\u5165\u3055\u308c\u305f\u578b\u30d2\u30f3\u30c8 (Type Hints) \u306f\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306b\u578b\u6ce8\u91c8 (\u578b\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3) \u3092\u8a18\u8ff0\u3059\u308b\u6a19\u6e96\u7684\u306a\u65b9\u6cd5\u3092\u5b9a\u7fa9\u3059\u308b\u3082\u306e\u3067\u3059. \u3053\u306e\u8a18\u6cd5\u306f mypy<\/a> \u3067\u5229\u7528\u3055\u308c\u3066\u3044\u305f\u3082\u306e\u306e\u5f71\u97ff\u3092\u5f37\u304f\u53d7\u3051\u3066\u304a\u308a, Python 3.0 \u3067\u5c0e\u5165\u3055\u308c\u305f\u95a2\u6570\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u5229\u7528\u3057\u307e\u3059. \u578b\u30d2\u30f3\u30c8\u306f\u6b21\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059:<\/p>\n

def add(x: int, y: int) -> int:\r\n    \"\"\"docstring here\"\"\"\r\n    return x + y\r\n<\/pre>\n

Python 3.5 \u306f\u69d8\u3005\u306a\u578b\u3092\u8a18\u8ff0\u3059\u308b\u305f\u3081\u306b typing \u30e2\u30b8\u30e5\u30fc\u30eb<\/a>\u304c\u8ffd\u52a0\u3055\u308c\u307e\u3057\u305f. \u3053\u306e typing \u30e2\u30b8\u30e5\u30fc\u30eb\u306f Python 3.5 \u4ee5\u524d\u3067\u3082\u30d0\u30c3\u30af\u30dd\u30fc\u30c8<\/a>\u304c\u5229\u7528\u3067\u304d\u307e\u3059.<\/p>\n

typing \u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u30d0\u30c3\u30af\u30dd\u30fc\u30c8\u304c\u5b58\u5728\u3057\u3066\u3082, Python 2 \u3067\u306f\u95a2\u6570\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u304c\u5229\u7528\u3067\u304d\u306a\u3044\u305f\u3081, Python 2\/3 \u4e21\u5bfe\u5fdc\u306e\u30b3\u30fc\u30c9\u3067\u95a2\u6570\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306b\u3088\u308b\u578b\u30d2\u30f3\u30c8\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u306f\u4e0d\u53ef\u80fd\u3067\u3057\u305f.<\/p>\n

\u8ffd\u8a18: \u95a2\u6570\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u7528\u3044\u306a\u3044\u578b\u6ce8\u91c8\u306e\u8a18\u8ff0\u65b9\u6cd5\u3068\u3057\u3066\u30b9\u30bf\u30d6\u30d5\u30a1\u30a4\u30eb<\/a>\u304c\u3042\u308a\u307e\u3059. \u3053\u308c\u306f Python \u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9 <source>.py<\/code> \u306b\u5bfe\u5fdc\u3059\u308b\u30b9\u30bf\u30d6\u30d5\u30a1\u30a4\u30eb <source>.pyi<\/code> \u306b\u578b\u6ce8\u91c8\u306e\u307f\u3092\u5225\u306b\u8a18\u8ff0\u3059\u308b\u3082\u306e\u3067\u3059.<\/p>\n

PEP 484<\/h2>\n

PEP 484 \u306f\u578b\u30d2\u30f3\u30c8\u306e\u8a18\u8ff0\u65b9\u6cd5\u3092\u5b9a\u7fa9\u3057\u305f PEP \u3067\u3059. PEP 484 \u306f\u4f55\u5ea6\u304b\u306e\u66f4\u65b0\u3092\u7d4c\u3066, Python 2.7 \u306b\u304a\u3051\u308b\u578b\u30d2\u30f3\u30c8\u306e\u8a18\u8ff0\u65b9\u6cd5\u306e\u63d0\u6848<\/strong><\/a> (suggested, not mandatory) \u304c\u8ffd\u8a18\u3055\u308c\u307e\u3057\u305f.<\/p>\n

Python 2 \u306b\u304a\u3051\u308b\u578b\u30d2\u30f3\u30c8\u306f\u00a0# type:<\/code> \u304b\u3089\u59cb\u307e\u308b1\u884c\u306e\u30b3\u30e1\u30f3\u30c8\u3068\u3057\u3066\u66f8\u304b\u308c\u307e\u3059. \u3053\u306e\u30b3\u30e1\u30f3\u30c8\u3092\u95a2\u6570\u5b9a\u7fa9\u306e\u76f4\u5f8c, docstring\u00a0\u306e\u524d\u306b\u8a18\u8ff0\u3057\u307e\u3059. \u5148\u307b\u3069\u306e Python 3 \u3067\u306e\u4f8b\u306f Python 2 \u3067\u306f\u6b21\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059.<\/p>\n

def add(x, y):\r\n    # type: (int, int) -> int\r\n    \"\"\"docstring here\"\"\"\r\n    return x + y\r\n<\/pre>\n

\u3053\u306e\u8a18\u6cd5\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u30c4\u30fc\u30eb\u3067\u306f Python 2\/3 \u306e\u5225\u306b\u3088\u3089\u305a, \u3053\u306e\u8a18\u6cd5\u306b\u5bfe\u5fdc\u3059\u308b\u3053\u3068\u304c\u6c42\u3081\u3089\u308c\u3066\u3044\u307e\u3059. \u3053\u308c\u306b\u3088\u308a Python 2\/3 \u4e21\u5bfe\u5fdc\u306e\u30b3\u30fc\u30c9\u3067\u3082\u578b\u30d2\u30f3\u30c8\u304c\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u3053\u3068\u304c\u671f\u5f85\u3055\u308c\u307e\u3059.<\/p>\n

\u8a18\u8ff0\u65b9\u6cd5\u306b\u3064\u3044\u3066\u306e\u6ce8\u610f<\/h3>\n

\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30e1\u30bd\u30c3\u30c9\u3068\u30af\u30e9\u30b9\u30e1\u30bd\u30c3\u30c9\u306e\u7b2c1\u5f15\u6570\u4ee5\u5916\u306e\u3059\u3079\u3066\u306e\u5f15\u6570\u306b\u578b\u6ce8\u91c8\u3092\u8a18\u8ff0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059.<\/p>\n

class TestClass(object):\r\n    def spam(self, message):\r\n        # type: (str) -> None\r\n        pass\r\n\r\n    @classmethod\r\n    def ham(cls, message):\r\n        # type: (str) -> None\r\n        pass\r\n<\/pre>\n

\u95a2\u6570\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u7528\u3044\u308b\u65b9\u6cd5\u3067\u306f\u8fd4\u308a\u5024\u306e\u578b\u6ce8\u91c8\u3092\u7701\u7565\u3059\u308b\u3068 Any<\/code> \u3092\u6307\u5b9a\u3057\u305f\u3082\u306e\u3068\u898b\u306a\u3055\u308c\u307e\u3059\u304c, \u3053\u306e\u8a18\u6cd5\u3067\u306f\u8fd4\u308a\u5024\u306e\u578b\u6ce8\u91c8\u3092\u7701\u7565\u3059\u308b\u3053\u3068\u306f\u51fa\u6765\u307e\u305b\u3093.<\/p>\n

def spam(x, y):\r\n    # type: (str, int) -> Any\r\n    pass\r\n<\/pre>\n

*args<\/code> \u3084 **kwargs<\/code> \u306e\u578b\u6ce8\u91c8\u306f\u5bfe\u5fdc\u3059\u308b\u578b\u306e\u524d\u306b *<\/code> \u3084 **<\/code> \u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u8a18\u8ff0\u3057\u307e\u3059.<\/p>\n

def ham(x, *eggs):\r\n    # type: (int, *str) -> str\r\n    pass\r\n<\/pre>\n

\u30c4\u30fc\u30eb\u306e\u5bfe\u5fdc\u72b6\u6cc1<\/h2>\n

mypy<\/h3>\n

2016\/2\/16 \u306b\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u305f mypy 0.3<\/a> \u3067 Python 2 \u3067\u306e\u578b\u30d2\u30f3\u30c8\u306e\u30b5\u30dd\u30fc\u30c8\u304c\u8ffd\u52a0\u3055\u308c\u307e\u3057\u305f.<\/p>\n

PyCharm<\/h3>\n

\u4eca\u5f8c\u30ea\u30ea\u30fc\u30b9\u4e88\u5b9a\u306e PyCharm 5.1 \u3067 Python 2 \u3067\u306e\u578b\u30d2\u30f3\u30c8\u306e\u30b5\u30dd\u30fc\u30c8\u304c\u8ffd\u52a0\u3055\u308c\u308b\u4e88\u5b9a<\/a>\u3067\u3059. \u8a18\u4e8b\u57f7\u7b46\u6642\u70b9\u3067\u306f PyCharm 5.1 \u306e EAP (Early Access Preview) \u3067\u8a66\u3059\u3053\u3068\u304c\u51fa\u6765\u307e\u3059.<\/p>\n

\u66f4\u65b0\u5c65\u6b74<\/h2>\n
    \n
  • 2016\/2\/29: \u30b9\u30bf\u30d6\u30d5\u30a1\u30a4\u30eb\u306b\u3064\u3044\u3066\u8ffd\u8a18<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"

    \u3053\u306e\u8a18\u4e8b\u3067\u306f PEP 484 — Type Hints \u306e\u66f4\u65b0\u306b\u3088\u3063\u3066\u63d0\u6848\u3055\u308c\u3066\u3044\u308b Python 2 \u306b\u304a\u3051\u308b\u578b\u30d2\u30f3\u30c8\u306b\u3064\u3044\u3066\u306e\u60c5\u5831\u3092\u307e\u3068\u3081\u307e\u3059.<\/p>\n","protected":false},"author":2,"featured_media":1303,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,3],"tags":[108,15,96],"_links":{"self":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/1294"}],"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=1294"}],"version-history":[{"count":16,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/1294\/revisions"}],"predecessor-version":[{"id":1308,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/1294\/revisions\/1308"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/media\/1303"}],"wp:attachment":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/media?parent=1294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/categories?post=1294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/tags?post=1294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}