Created
January 3, 2020 05:34
-
-
Save gwsu2008/458d9c825e5131f759aa0747595d8b37 to your computer and use it in GitHub Desktop.
Python - Abstract Class
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
""" | |
Python - Abstract Class | |
Yes. Python has Abstract Base Class module allows to enforce that a derived class implements a particular method using a special @abstractmethod decorator on that method. | |
""" | |
from abc import ABCMeta, abstractmethod | |
class Animal: | |
__metaclass__ = ABCMeta | |
@abstractmethod | |
def say_something(self): return "I'm an animal!" | |
class Cat(Animal): | |
def say_something(self): | |
s = super(Cat, self).say_something() | |
return "%s - %s" % (s, "Miauuu") | |
>>> c = Cat() | |
>>> c.say_something() | |
#"I'm an animal! - Miauu" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment