Skip to content

Instantly share code, notes, and snippets.

@rmyers
Created July 10, 2013 14:07
Show Gist options
  • Save rmyers/5966576 to your computer and use it in GitHub Desktop.
Save rmyers/5966576 to your computer and use it in GitHub Desktop.
Metaclass example
import os
class DebianPackagerMixin:
def boo(self):
print('debian')
class RedhatPackagerMixin:
def boo(self):
print('redhat')
class DefaultPackage:
def boo(self):
print('default')
class BasePackage(type):
def __new__(meta, name, bases, dct):
if os.path.isfile("/etc/debian_version"):
bases += (DebianPackagerMixin, )
elif os.path.isfile("/etc/redhat-release"):
bases += (RedhatPackagerMixin, )
else:
bases += (DefaultPackage,)
return super(BasePackage, meta).__new__(meta, name, bases, dct)
class Package(object):
__metaclass__ = BasePackage
# common stuff here
if __name__ == '__main__':
p = Package()
p.boo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment