tags:

views:

25

answers:

1
from module import * # adds 'BlahRenderer', 'FooRenderer', 'BarRenderer', 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?

+3  A: 

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, ...}
KennyTM
+1. I prefer the second option (`renderers`) myself.
Manoj Govindan