locals() and dictionaries

Discussion in 'Python' started by JerryB, Feb 1, 2006.

  1. JerryB

    JerryB Guest

    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"""
    JerryB, Feb 1, 2006
    #1
    1. Advertising

  2. 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
    Rocco Moretti, Feb 1, 2006
    #2
    1. Advertising

  3. JerryB

    JerryB Guest

    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
    JerryB, Feb 1, 2006
    #3
  4. 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
    Alex Martelli, Feb 2, 2006
    #4
  5. JerryB

    Kent Johnson Guest

    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
    Kent Johnson, Feb 2, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olivier Sessink
    Replies:
    0
    Views:
    378
    Olivier Sessink
    Feb 24, 2005
  2. Faisal Alquaddoomi

    Globals, Locals Dictionaries + Callbacks Question

    Faisal Alquaddoomi, Dec 21, 2005, in forum: Python
    Replies:
    3
    Views:
    352
    Steve Holden
    Dec 22, 2005
  3. Josiah Carlson
    Replies:
    4
    Views:
    342
    Boris Borcic
    Jun 19, 2006
  4. lysdexia
    Replies:
    6
    Views:
    469
    John Machin
    Dec 2, 2007
  5. Brandon
    Replies:
    12
    Views:
    467
    Brandon
    Aug 15, 2008
Loading...

Share This Page