I have the following setup in one module:
class A(object):
# stuff
class B(object):
# stuff
Now what I want to do is, creating an instance of class A by name (I just have the class name as a string) inside of B. How can I do this avoiding the globals function?
Why not just use
A? Or do you have just a string'A'? If yes,globals()['A']is the way to go. The alternative would begetattr(sys.modules[__name__], 'A')but obviouslyglobals()is more appropriate.So by just looking at the instructions used for the various ways to access
Foo, usingglobals()is most likely faster than going throughsys.modules.