Are there any major differences between the two approaches?
Yes. The answer includes a concept called metaclasses .
[Metaclasses] is a deeper magic than 99% of users should ever worry about. If you are interested in whether you need them, you do not need it (people who really need them know for sure that they need them, and do not need to explain why). Pe Tim Peters, author of Zen of Python ( source )
If you think that you are in these 99%, then do not read further, do not hesitate to just use type . This is as good as types.new_class , except in the rare situation when you use metaclasses.
If you want to learn more about metaclasses, I suggest you take a look at some of the high-quality answers published on the topic “ What are metaclasses in Python? ”. (I recommend it .)
Once you understand what a metaclass is, the answer becomes obvious. Since type is a specific metaclass, it will only work if you want to create classes that use it as their metaclass.
However, if you want to use a non-default metaclass
class MySimpleMeta(type): pass
and the static class will not do
class MyStaticClass(object, metaclass=MySimpleMeta): pass
then you can use types.new_class
import types MyStaticClass = types.new_class("MyStaticClass", (object,), {"metaclass": MyMeta}, lambda ns: ns)
(In short, the reason it requires a called (e.g. lambda ns: ns ) instead of a dictionary is because metaclasses are allowed to take care of the order in which the attributes are defined. )