I would like to define globals in a "programmatic" way. Something similar to what I want to do would be:
definitions = {'a': 1, 'b': 2, 'c': 123.4}
for definition in definitions.items():
exec("%s = %r" % definition) # a = 1, etc.
Specifically, I want to create a module fundamentalconstants
that contains variables that can be accessed as fundamentalconstants.electron_mass
, etc., where all values are obtained through parsing a file (hence the need to do the assignments in a "programmatic" way).
Now, the exec
solution above would work. But I am a little bit uneasy with it, because I'm afraid that exec
is not the cleanest way to achieve the goal of setting module globals.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…