What is a non-deprecated way to do `new.classobj(name, (base_class,), my_dict)`

风格不统一 提交于 2019-12-20 04:22:44

问题


I found in saucelabs example.py file the following code snippet using the import new module.

import new
# .. snip ..
def on_platforms(platforms):
    def decorator(base_class):
        module = sys.modules[base_class.__module__].__dict__
        for i, platform in enumerate(platforms):
            d = dict(base_class.__dict__)
            d['desired_capabilities'] = platform
            name = "%s_%s" % (base_class.__name__, i + 1)
            module[name] = new.classobj(name, (base_class,), d)
    return decorator

However reading the help(new) shows that that module is deprecated. What is a non-depreciated way to do new.classobj(name, (base_class,), d)


回答1:


The type keyword is a class. You can perform the above using

module[name] = type(name, (base_class,), d)

This will return a class of name name with the tuple parameters of the base class base_class, initializing the member objects with the dict d.



来源:https://stackoverflow.com/questions/23450529/what-is-a-non-deprecated-way-to-do-new-classobjname-base-class-my-dict

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!