Skip to content

Instantly share code, notes, and snippets.

@gwsu2008
Created January 3, 2020 05:34
Show Gist options
  • Save gwsu2008/458d9c825e5131f759aa0747595d8b37 to your computer and use it in GitHub Desktop.
Save gwsu2008/458d9c825e5131f759aa0747595d8b37 to your computer and use it in GitHub Desktop.
Python - Abstract Class
"""
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