问题
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