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

StackOverflow https://stackoverflow.com/questions/23450529

  •  14-07-2023
  •  | 
  •  

Domanda

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)

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top