Last active
November 29, 2015 22:37
-
-
Save brianray/a0bafec44822b844ee66 to your computer and use it in GitHub Desktop.
Abstract Base Class Decorator 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
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