locals() and dictionaries

JerryB grblanco at gmail.com
Wed Feb 1 13:24:18 EST 2006


Hi,
I have a dictionary, a string, and I'm creating another string, like
this:

dict = {}
dict[beatles] = "need"
str = "love"

mystr = """All you %(dict[beatles])s is %(str)s""" % locals()

Why do I get
keyerror: 'dict[one]'?

Is there a way to reference the elements in a dictionary with locals()
or do I need to create a temp variable, like

need = dict[one]
mystr = """All you %(need)s is %(str)s"""




More information about the Python-list mailing list