Created
July 10, 2013 14:07
-
-
Save rmyers/5966576 to your computer and use it in GitHub Desktop.
Metaclass example
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 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