Skip to content

Instantly share code, notes, and snippets.

@brianray
Last active November 29, 2015 22:37
Show Gist options
  • Save brianray/a0bafec44822b844ee66 to your computer and use it in GitHub Desktop.
Save brianray/a0bafec44822b844ee66 to your computer and use it in GitHub Desktop.
Abstract Base Class Decorator Python
import inspect
def abstractmethod(mthd):
def default_abstract_method(*args, **kwargs):
call_args = inspect.getcallargs(mthd, *args, **kwargs)
arg_spec = inspect.getargspec(mthd)
class_name = call_args['self'].__class__.__name__
error_str = "{}.{}({}) method must be defined"
raise NotImplementedError(error_str.format(class_name,
mthd.func_name,
arg_spec))
default_abstract_method.__name__ = mthd.__name__
return default_abstract_method
class A:
@abstractmethod
def doit(self):
pass
class B(A):
pass
x = B()
x.doit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment