Created
November 19, 2021 21:52
-
-
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
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
#!/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