Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > locals() and dictionaries

Reply
Thread Tools

locals() and dictionaries

 
 
JerryB
Guest
Posts: n/a
 
      02-01-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"""

 
Reply With Quote
 
 
 
 
Rocco Moretti
Guest
Posts: n/a
 
      02-01-2006
JerryB wrote:
> 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:

>>> dict[beatles] = "need"


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
 
Reply With Quote
 
 
 
 
JerryB
Guest
Posts: n/a
 
      02-01-2006
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

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      02-02-2006
JerryB <> wrote:
...
> 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
 
Reply With Quote
 
Kent Johnson
Guest
Posts: n/a
 
      02-02-2006
JerryB wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
updating dictionaries from/to dictionaries Brandon Python 12 08-15-2008 12:35 AM
Pickling dictionaries containing dictionaries: failing,recursion-style! lysdexia Python 6 12-02-2007 12:03 AM
Jazzy and Aspell dictionaries jch.helary@free.fr Java 1 01-09-2006 05:28 PM
Dictionaries for English-French and English-Spanish fkissam Computer Support 2 07-14-2004 09:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57