Skip to content

Instantly share code, notes, and snippets.

@PurityLake
Last active September 27, 2019 16:41
Show Gist options
  • Save PurityLake/de54d0900c259449795ee005e4af8541 to your computer and use it in GitHub Desktop.
Save PurityLake/de54d0900c259449795ee005e4af8541 to your computer and use it in GitHub Desktop.
An implementation of an Abstract Base Class in python
def makeFun(name, *args):
exec("""def {}({}): raise NotImplementedError("'{}' is not implemented!")""".format(name, ', '.join(args), name))
return locals()[name]
class ABC(type):
def __init__(cls, name, bases, dct):
for k, v in cls.__dict__.items():
if not k.startswith("__") and callable(v):
setattr(cls, k, makeFun(k, *v.__code__.co_varnames))
class Class(metaclass=ABC):
def __init__(self, **kwargs):
pass
def shouldFail(self, a, b):
pass
a = Class()
try:
a.shouldFail(1, 2)
except NotImplementedError as e:
print(str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment