Created
May 12, 2015 22:16
-
-
Save bfueldner/737fdc91482a1fbade62 to your computer and use it in GitHub Desktop.
Automatic class registration using metaclass
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
#!/usr/bin/python | |
reg = {} | |
class metaclass_register(type): | |
def __init__(cls, name, bases, nmspc): | |
super(metaclass_register, cls).__init__(name, bases, nmspc) | |
reg[name] = cls | |
class class1(object): | |
__metaclass__ = metaclass_register | |
def foo(self): | |
print "foo" | |
class class2(class1): | |
def foo(self): | |
print "foo2" | |
print reg | |
t = reg['class1']() | |
t.foo() | |
t = reg['class2']() | |
t.foo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment