On Thu, 09 Mar 2006 01:42:55 -0800, Petr Jakes wrote:
In my code I have relatively wide dictionary definition (about 100
rows).
I would like to put it in to the different file (module) because of the
main code readability (say the name of the file will be
"my_dictionary.py")
In the dictionary I have strings formatted using % operator like:
lcd={2:"Your credit= %3d" % (credit)}
The values in the dictionary (e.g. "Your credit= 9.99", or whatever value
credit actually has) are fixed at creation. I assume that means that
credit etc. are also fixed values.
While I am trying to import my_dictionary in to the main code, I am
getting:
exception unhandled NameError
name "credit" is not defined
How can I organize my code so the "credit" variable will be "visible"
in the "my_dictionary" namespace?
Put it in the same module as my_dictionary.
E.g.
# Module my_dictionary.py
# which I hope will have a more sensible name before being used
# for production-code
credit = 27
foo = 15
lcd = {2: "Your credit= %3d" % credit}
On the other hand, if credit is a calculated value, this might not be
an easy thing to do. In that case, you can do this:
# Module calculatevalues.py
credit = some_function()
foo = some_other_function()
# Module my_dictionary.py
import calculatevalues
lcd = {2: "Your credit= %3d" % calculatevalues.credit}
On the third hand, if the strings from the dictionary are supposed to be
changed at run-time (which sounds more sensible to me) then do this:
# Module my_dictionary.py
lcd = {2: "Your credit= %3d"}
# main program
import my_dictionary
.... lots of code here
credit = 27
.... more code
print lcd[2] % credit
--
Steven.