Python动态创建类

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#author: D.Lucifer
def create_object(object_attribute):
    class o: pass
    if '#class' in object_attribute.keys():
        (module_name, class_name) = object_attribute['#class'].rsplit('.', 1)
        module_meta = __import__(module_name) 
        class_meta = getattr(module_meta, class_name) 
        o = class_meta()
    for k in object_attribute:
        if str(type(object_attribute[k])) == '<class \'dict\'>':
            setattr(o, k, create_object(object_attribute[k]))
        else:
            setattr(o, k, object_attribute[k])
    return o