Skip to content

Instantly share code, notes, and snippets.

@jeamland
Created August 20, 2012 02:25
Show Gist options
  • Save jeamland/3399471 to your computer and use it in GitHub Desktop.
Save jeamland/3399471 to your computer and use it in GitHub Desktop.
Enforced abstract base classes
class AbstractBaseMetaClass(type):
def __new__(meta, classname, bases, attributes):
if getattr(meta, '_required_methods', None) is None:
meta._required_methods = []
for name, attribute in attributes.items():
if name == '__metaclass__':
continue
if callable(attribute):
meta._required_methods.append(name)
else:
for required_method in meta._required_methods:
if required_method not in attributes:
raise Exception('OMG')
return type.__new__(meta, classname, bases, attributes)
class AbstractBaseClass(object):
__metaclass__ = AbstractBaseMetaClass
def required_method(self):
pass
class CorrectClass(AbstractBaseClass):
def required_method(self):
pass
class IncorrectClass(AbstractBaseClass):
def optional_method(self):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment