Question

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)

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top