What is the advantage in using `exec` over `type()` when creating classes at runtime?
问题 I want to dynamically create classes at runtime in python. For example, I want to replicate the code below: >>> class RefObj(object): ... def __init__(self, ParentClassName): ... print "Created RefObj with ties to %s" % ParentClassName ... class Foo1(object): ... ref_obj = RefObj("Foo1") ... class Foo2(object): ... ref_obj = RefObj("Foo2") ... Created RefObj with ties to Foo1 Created RefObj with ties to Foo2 >>> ... but I want the Foo1, Foo2, Foo classes to be created dynamically (ie: during