Skip to content

Instantly share code, notes, and snippets.

@eskerda
Created June 3, 2013 05:04
Show Gist options
  • Save eskerda/5696208 to your computer and use it in GitHub Desktop.
Save eskerda/5696208 to your computer and use it in GitHub Desktop.
Kids, do not try this at home!
#!/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