Last active
September 27, 2019 16:41
-
-
Save PurityLake/de54d0900c259449795ee005e4af8541 to your computer and use it in GitHub Desktop.
An implementation of an Abstract Base Class in python
This file contains 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
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