Skip to content

Instantly share code, notes, and snippets.

@creisor
Created November 19, 2021 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creisor/a14611f97ace29afed8a20e30ad8eddd to your computer and use it in GitHub Desktop.
Save creisor/a14611f97ace29afed8a20e30ad8eddd to your computer and use it in GitHub Desktop.
Here's a simple example of how one can use the abc (Abstract Base Class) library in Python3 as an alternative to "raise NotImplementedError" when creating abstract classes
#!/usr/bin/env python3
import abc
class Task(abc.ABC):
@abc.abstractmethod
def do(self):
pass
@abc.abstractmethod
def rollback(self):
pass
class CoolTask(Task):
def __init__(self, name):
self.name = name
def do(self, description):
print(f'{self.name}: doing task')
print(f'description: {description}')
def rollback(self, description):
print(f'{self.name}: rolling back task')
print(f'description: {description}')
class YellTask(Task):
def __init__(self, name):
self.name = name
def do(self):
print(f'{self.name.upper()}: DOING TASK')
def rollback(self):
print(f'{self.name.upper()}: ROLLING BACK TASK')
t = CoolTask("my cool task")
t.do("prints what I'm doing")
t.rollback("prints what I'm rolling back")
y = YellTask("my yelling task")
y.do()
y.rollback()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment