locals() and dictionaries

J

JerryB

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"""
 
R

Rocco Moretti

JerryB said:
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"""

1) Avoid variable names like 'dict' and 'str'- they cover up the builtin
names.

2) When showing error, don't retype - cut and paste:

Traceback (most recent call last):
File "<pyshell#6>", line 1, in -toplevel-
dict[beatles] = "need"
NameError: name 'beatles' is not defined
>>> dict['beatles'] = "need"
>>>

3) In string formating, the item in parenthesis, used as a string, is
the key for the dictionary. That is:

"""All you %(dict[beatles])s is %(str)s""" % ld

is the same as

"""All you %s is %s""" % (ld['dict[beatles]'],ld['str'])

4) Your best bet is not to use locals(), but to create a new dictionary
with the appropriate keys. E.g.:
>>> d = {}
>>> d['beatles'] = "need"
>>> s = "love"
>>> d2 = d.copy()
>>> d2['str'] = s
>>> d['str']

Traceback (most recent call last):
File "<pyshell#24>", line 1, in -toplevel-
d['str']
KeyError: 'str'
>>> d2['str'] 'love'
>>> mystr = """All you %(beatles)s is %(str)s""" % d2
>>> print mystr
All you need is love
 
J

JerryB

Rocco:
thanks for your response. The examples were just made up. I don't
normally use 'dict' and 'str'.
I know I can create a dictionary with the variables I want, etc. My
question is not how to solve the problem, or how to come up with a
work-around (I'm getting pretty good at this one :), so my question
stands:

is it possible to access the individual members of a dictionary using %
locals() when creating a string?

Thank you again for your suggestions.
Jerry
 
A

Alex Martelli

JerryB said:
is it possible to access the individual members of a dictionary using %
locals() when creating a string?

Not by using the built-in locals(); you'd have to override locals to
mean someting different (not recommended).


Alex
 
K

Kent Johnson

JerryB said:
Rocco:
thanks for your response. The examples were just made up. I don't
normally use 'dict' and 'str'.
I know I can create a dictionary with the variables I want, etc. My
question is not how to solve the problem, or how to come up with a
work-around (I'm getting pretty good at this one :), so my question
stands:

is it possible to access the individual members of a dictionary using %
locals() when creating a string?

You might want to use a more powerful templating engine, for example
http://cheetahtemplate.org/ and probably many others.

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top