Python 3 Cookbook's "Defining Classes Programmatically" Recipe


Hi Dave,

The Python 3 Cookbook’s “Defining Classes Programmatically” recipe advises using types.new_class() and discusses the benefits over using type().

But it doesn’t compare against doing something like this does, where you define a (parametric) class inside a function and then return the new class that was created. Is that because you don’t consider that programmatic class creation?

In any case, I’d be curious to hear what you think about how this approach compares to using types.new_type().

One downside of types.new_type() is that it requires Python 3.3+. I guess the Python 3 Cookbook assumes that too, but for a library like bidict that still maintains Python 2 compatibility, it’s unfortunately not an option.