Using the "exec" function would be easiest way of doing this:
CLASS_TEMPLATE = """\
class %s(peeww.Model):
pass
"""
classname = "cats"
exec(CLASS_TEMPLATE % classname)
c = cats() # works !
Explanation:
I've created a string named CLASS_TEMPLATE
which contains Python code creating a class. I use a format specifier %s
as a class name, so it can be replaced.
Doing this:
CLASS_TEMPLATE % "cats"
replaces the format specifier %s
and creates a string that contains the code to create a class named "cats".
If you ask Python to execute this code, your class gets defined:
exec(CLASS_TEMPLATE % "cats")
You can see that it exists in your namespace:
>>> globals()
{..., 'cats': <class __main__.cats at 0x1007b9b48>, ...}
... and that it is a class:
>>> type(cats)
<type 'classobj'>
And you can start using this newly defined class to instantiate objects:
c = cats()