PyConJPã§è§¦çºããã¦Pythonã®Type Hintsï¼åãã³ãï¼ã«ã¤ãã¦æ¸ãã¦ã¿ã
çããããã«ã¡ã¯
ãå
æ°ã§ãããã©ã¼ã¡ã³é£ã¹ãããªã£ã¦ããã
ç§èªèº«ã¯èªä»å
±ã«èªããããªPython2.7.x userã®ã¤ãããªã®ã§ããã
PyConJP2016ã§Andrey Vlasovskikhæ°ã®è¬æ¼ã§Type Hintsï¼åãã³ãï¼ã®è©±ããã£ã¦
Python3.5ãããã¨ãæããªããèãã¦ã¾ããã
ã¶ã£ã¡ããåãã³ãä½ãç¥ãã¾ããã
ã¨ãããã¨ã§ãä»åã¯Pythonã«ãããType Hintsã調ã¹ã¦ã¿ã¾ããã
Type Hintsã«ã¤ãã¦
ããããType Hintsã£ã¦ãªã«ï¼
åã«å¯¾ããã¢ããã¼ã·ã§ã³ã§ããå ·ä½çã«ã¯å®ä¾ãè¦ãã¨ããããããããããã§ãã
PEP484ã«è¦å®ãããType Hintsãæä¾ããããããPython3.5ã§å°å
¥ããã¦ãã¾ãã
ä½ã®ãã¨ããããããªãã®ã§ãæ©éèªãã§ã¿ããã¨æã£ããæå¤ã«é·ãã£ãã®ã§èªãã¦ããã¾ããã
www.python.org
ä½ãå¬ããã®ã
調ã¹ãã¨ã©ããããããªã¡ãªãããããããã§ãã
- Type Hintsã使ããã¨ã§éç解æã容æã«ããã
- 容æãªãªãã¡ã¯ã¿ãªã³ã°ãããã¥ã¡ã³ãã¼ã·ã§ã³ã®ä½æ
- åã¢ããã¼ã·ã§ã³ã®æ¨æºçãªæ¸ãæ¹ã®å®ç¾©
調ã¹ã¦ããã®ã§ããããã®ãããã§ãã¾ãè¦æ
£ããªãååãªã®ã§ãä½ãè¨ã£ã¦ãããã ããã¨ããã£ã¦ãã¦ã¾ããã»ã»ã»
ã¨ãããã¨ã§ãããããå®ä¾ã
å®ä¾
ãããããã¥ã¼ããªã¢ã«ã®ä¾
ãã¦ãæ¢ã«èª¿ã¹ãã®ã«ç²ããã®ã§Python3.5ã«æ¸ãã¦ããä¾ã«è§¦ã£ã¦ã¿ã¾ãããã
def greeting(name: str) -> str: return 'Hello ' + name
name:strããnameå¤æ°ãstråã§ãããã¨ã -> strãè¿ãå¤ãstrãæå¾ ããã¨ãã£ããã¨ã示ãã¦ãã¾ãã
å¤æ°ã®å¥å
å¤æ°ã¯åã®ç´¹ä»ã®strã ãã§ã¯ãªããè¤æ°ã®åãå®ç¾©ãããã¨ãã§ãã¾ãã
次ã«ãã®ä¾ãæ²è¼ãã¾ãã
å ã¿ã«ãTupleçã使ã£ã¦ãå®ç¾©ãããã¨ãã§ãã¾ãã
from typing import TypeVar, Tuple T = TypeVar('T', int, float, complex) def typetest(var: Tuple[T, T]) -> Tuple[T, T]: return var
Collections
Collectionsã使ãå ´åã¯typingã®ListãSetã使ããã¨ãã§ãã¾ãã
以ä¸ã«ã¯ä¾ãè¨è¼ãã¾ãã
# coding:utf-8 from typing import TypeVar, List T = TypeVar('T', int, float, complex) def collection_test(list_var: List[T]) -> T: return list_var[0]
Generics
Genericsã使ãã¨ãã¦ã¼ã¶å®ç¾©ã®Genericsãæ¸ããã¨ãã§ãã¾ãã
ããã¾ã§æ¥ãã¨C++ã®templateæ§æãæãåºãã¦ãã¾ããã
ãããªãã®ãæ¸ããã¨ãã§ãã¾ãã
# coding:utf-8 from typing import TypeVar, Generic T = TypeVar('T', int, float, complex) class GenericTest(Generic[T]): def __init__(self,init: T): self.init = init def get_init(self) -> T: return self.init def set_init(self, init: T): self.init = init
æ¸ãã¦ã¿ããã©ãã©ããªå¹æãããã®ã§ããã
ããã¾ã§ããã¤ã試ãã«æ¸ãã¦ã¿ã¾ãããã
å®éã«ã©ããªå¹æãããã®ãã確èªãããã®ã§ãè¦ã¦ã¿ã¾ãããã
PyCharm
PyCharmã®Version5.0以éã§Type Hintsããããã¨ã«ãã£ã¦ã
è¦åãåºãããã«ãªãã¾ãã
æ¸ãã¦ããæã«ãå®éã«èªåãæå¾
ãã¦ããªãåãå
¥åãã¦ãããã¨ããããã®ã§ã
ãã®å°ç¹ã§è¦åãåºãã¦ããã¾ããè¶
便å©
C++ã¨ãæ¸ãã¦ãã人ã ã¨ãçµæ§ããããããããªæãã§ã¯ãªãã§ããããã
Mypy
Mypyã¯çµé¨çãªéçãã§ãã¯ã®ãã¼ã«ã§ãåçãéçã®ã¿ã¤ãã³ã°ã®è¯ãã¨ããåãããçºã«éçºããããã§ãã
Pythonã®å¼·åãã¤ã便å©ãªåãã§ãã¯ã§ãåºæ¬çãªPythonããã°ã©ã ããµãã¼ããã¦ããããã§ãã
Mypyãå
ã«ä½æããã¦ãType Hintsã¨ãã¦å°å
¥ãããããã§ãã
ã¤ã³ã¹ãã¼ã«ã¯æ¬¡ã®ã³ãã³ãã§ããç§ã¯ç¥ãã¾ããããmypyã¨å¼ã°ããpipã§åå¾ã§ããã½ããã¦ã§ã¢ã¯å¥ã®å
容ã¨ã®ãã¨ã§ãã
pip install mypy-lang
ãã¦ã試ãã«æ¬¡ã®ã³ã¼ãã«å¯¾ãã¦mypyãæ¸ãã¦ã¿ããã¨æãã¾ãã
åãåããªããgreetingã«42ã®intã使ã£ã¦ãã¾ãã
# coding:utf-8 def greeting(name:str) -> str: return 'Hello, {}'.format(name) greeting(42)
次ã®ã³ã¡ã³ããåºã¦ãã¾ãã
mypy TypeHints/typehint.py TypeHints/typehint.py:6: error: Argument 1 to "greeting" has incompatible type "int"; expected "str"
ã¤ã¾ããgreetingé¢æ°ã«å¯¾ãã¦ã第ä¸å¼æ°ã¯strãæå¾
ãã¦ãããã
intãä¸ãã¦ããã¨è¨ããã¦ãã¾ãã
ããã§æå¾
ãã¦ããªãå¤ãæå
¥ãã¦ãããã¨ããããã¾ãã
æå¾ã«
Type Hintsã¯é¢ç½ãããã©ãversion2,3é©ç¨ãé£ãããã ããå°ãªãã¨ãå¾æ°å¹´ã¯ä½¿ããªãããª
ã©ãããAndrey Vlasovskikhæ°æ°ããPython2ã®ã»ããPyCharmçã«ã¯å©ç¨è
ãå¤ããããªã®ã§ã
Python3ããããããã¾ãã人æ°ã«ãªã£ãã¨ãã«ã©ããªããã«ãã£ã¦Type Hintsã®æ±ããã©ããããã¨ãã
ã³ã¼ãã£ã³ã°è¦ç´ãå¤ãã£ã¦ããã®ããªã¨æã£ããããã¾ãã
åèã«Type Hintsã«è§¦ãã¦ããè¦ç´ããã£ããè¦ã¦ã¿ããã¨ãæã£ã¦ãã¾ãã