Created
December 17, 2019 06:39
-
-
Save AntiKnot/d55ea767e443f0147252e94140f05a5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import abc | |
| class Animal(metaclass=abc.ABCMeta): | |
| @abc.abstractmethod | |
| def screaming(self): | |
| 'Return when animal screaming the sound hear likes' | |
| return NotImplemented | |
| @abc.abstractmethod | |
| def walk(self, x, y): | |
| 'Make animal walk to position (x, y).' | |
| return NotImplemented | |
| class Dog(Animal): | |
| pass | |
| Dog() | |
| """ | |
| TypeError: Can't instantiate abstract class Dog with abstract methods screaming, walk | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Animal(object): | |
| def screaming(self): | |
| 'Return when animal screaming the sound hear likes' | |
| return NotImplemented | |
| def walk(self, x, y): | |
| 'Make animal walk to position (x, y).' | |
| return NotImplemented | |
| class Dog(Animal): | |
| pass | |
| Dog() | |
| """ | |
| Dog not NotImplemented screaming walk method, but can instantiate | |
| """ |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
python中abc的应用场景
ref
abc — 抽象類別 — 你所不知道的 Python 標準函式庫用法 03