How do you grab a value from the scope from a string
from module import * # adds 'BlahRenderer', 'FooRenderer', 'BarRend开发者_开发技巧erer', etc.
class MyClass
def __init__(self, value)
renderer = "%sRenderer" % value
self.RendererClass = ????
I know this can be done by doing the import inside __init__
and then doing locals()[renderer]
but how do I do it if the import is at the top?
Try globals()
instead of locals()
.
Although it may be better that your module
defines a dictionary or Factory to map each value
into a Renderer e.g.
renderers = {'Blah': BlahRenderer, 'Foo': FooRenderer, ...}
精彩评论