Created
June 3, 2013 05:04
-
-
Save eskerda/5696208 to your computer and use it in GitHub Desktop.
Kids, do not try this at home!
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/env python2.7 | |
""" class_generation.py: This file generates subclasses of | |
GeneralPurposeClass in execution time just for the fun | |
of it. | |
>>> import class_generation | |
>>> for class_name in class_generation.__all__: | |
... instance = eval("class_generation.%s" % class_name)() | |
... instance.greet() | |
... | |
GeneralPurposeClass: GeneralPurposeClass reporting for duty! | |
FooClass: I just say hello! | |
BarClass: reporting for duty is boring! | |
>>> | |
""" | |
__all__ = ['GeneralPurposeClass'] | |
class GeneralPurposeClass(object): | |
greeting = "GeneralPurposeClass reporting for duty!" | |
def greet(self): | |
print "%s: %s" % (self.__class__.__name__, self.greeting) | |
class_definition = [ | |
{ | |
'class_name': 'FooClass', | |
'greeting': 'I just say hello!' | |
}, | |
{ | |
'class_name': 'BarClass', | |
'greeting': 'reporting for duty is boring!' | |
} | |
] | |
for class_declaration in class_definition: | |
vars()[class_declaration['class_name']] = type( | |
class_declaration['class_name'], | |
(GeneralPurposeClass, ),{ | |
'greeting': class_declaration['greeting'] | |
} | |
) | |
__all__.append(class_declaration['class_name']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment