開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Head First はじめてのプログラミング ―頭とからだで覚えるPythonプログラミング入門 (Eric Freeman(著)、嶋田 健志(監修)、木下 哲也(翻訳)、株式会社オライリー・ジャパン)を12章(オブジェクト指向プログラミング - オブジェクト村への旅)の自分で考えてみよう(533ページ)の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3
from unittest import TestCase, main
from typing import List
class DogTest(TestCase):
def setUp(self) -> None:
pass
def tearDown(self) -> None:
pass
def test_human_years(self) -> None:
codie: Dog = Dog('Codie', 12, 38)
jackson: Dog = Dog('Jackson', 9, 12)
human_years_list: List[int] = [84, 63]
for dog, human_years in zip([codie, jackson], human_years_list):
self.assertEqual(dog.human_years(), human_years)
class Dog:
def __init__(self, name: str, age: float, weight: int):
self.name: str = name
self.age: float = age
self.weight: int = weight
def human_years(self) -> int:
return int(self.age) * 7
if __name__ == '__main__':
main()
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))
$ ./sample1.py
E
======================================================================
ERROR: test_human_years (__main__.DogTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./sample1.py", line 13, in test_human_years
codie: Dog = Dog('Codie', 12, 38)
NameError: name 'Dog' is not defined
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
$ mypy sample1.py
sample1.py:15: error: Bracketed expression "[...]" is not valid as a type
sample1.py:15: note: Did you mean "List[...]"?
$ mypy sample1.py
$ ./sample1.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
$
0 コメント:
コメントを投稿