I need to instantiate some keys of a Dict as a standalone global variables.
(Before you object let me mention that this is purely for testing purposes and i know it pollutes the global scope. It is not intended as programing technique.)
OK. Now that out of the way. If I use simple function like the one below it works.
def items_as_vars(obj, lst) :
for i in lst :
globals()[i] = obj.g(i)
print i, obj.g(i)
items_as_vars(obj, ['circle','square'])
print circle
but the moment I move this to become class method in a separate file it stops working i.e.
class blah:
@staticmethod
def items_as_vars(obj, lst) :
for i in lst :
globals()[i] = obj.g(i)
print i, obj.g(i)
blah.items_as_vars(obj, ['circle','square'])
print circle
NameError: name 'circle' is not defined
any idea why ? By "stop working", I mean the global variables are no longer instantiated.
More info: It seems to work when the class is in the same file, but not when the class is imported !
modified to static method, it is the same behaviour.