開発環境
- 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章(オブジェクト指向プログラミング - オブジェクト村への旅)の自分で考えてみよう(539ページ)の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3 from unittest import TestCase, main from typing import List class CakeTest(TestCase): def setUp(self): pass def tearDown(self): pass def test_simple_cake(self): simple_cake: Cake = Cake() self.assertNotIsInstance(simple_cake, BirthdayCake) self.assertNotIsInstance(simple_cake, FrostedCake) self.assertIsInstance(simple_cake, Cake) def test_chocolate_cake(self): chocolate_cake: FrostedCake = FrostedCake() self.assertIsInstance(chocolate_cake, Cake) self.assertIsInstance(chocolate_cake, FrostedCake) self.assertNotIsInstance(chocolate_cake, BirthdayCake) def test_bills_birthday_cake(self): bills_birthday_cake: BirthdayCake = BirthdayCake() for cls in [FrostedCake, Cake, BirthdayCake]: self.assertIsInstance(bills_birthday_cake, cls) class Cake: def __init__(self): self.flavor = None def bake(self): pass def cut(self): pass def eat(self): pass class FrostedCake(Cake): def __init__(self): super().__init__() self.frosting = None def frost(self): pass class BirthdayCake(FrostedCake): def __init__(self): super().__init__() self.name_on_cale = None def add_name(self): pass def add_candles(self): pass if __name__ == '__main__': main()
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))
$ ./sample2.py EEE ====================================================================== ERROR: test_bills_birthday_cake (__main__.CakeTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "./sample2.py", line 26, in test_bills_birthday_cake bills_birthday_cake: BirthdayCake = BirthdayCake() NameError: name 'BirthdayCake' is not defined ====================================================================== ERROR: test_chocolate_cake (__main__.CakeTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "./sample2.py", line 20, in test_chocolate_cake chocolate_cake: FrostedCake = FrostedCake() NameError: name 'FrostedCake' is not defined ====================================================================== ERROR: test_simple_cake (__main__.CakeTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "./sample2.py", line 14, in test_simple_cake simple_cake: Cake = Cake() NameError: name 'Cake' is not defined ---------------------------------------------------------------------- Ran 3 tests in 0.001s FAILED (errors=3) $ ./sample2.py ... ---------------------------------------------------------------------- Ran 3 tests in 0.000s OK $
0 コメント:
コメントを投稿