FAQ

ckrieg> for c in range(1, 3):
ckrieg> print "%(c)s %(1)s" % (locals(), dict01)

ckrieg> Results in an error: TypeError: format requires a mapping

ckrieg> Why doesn't the second loop work? It seems to be a
cleaner way
ckrieg> of doing this. Am I missing something?

The right-hand argument to the % operator is a tuple of
dictionaries, not a
dictionary.

Try something like this instead:

class MultiDict(dict):
def __init__(self, *args):
self._dicts = args

def __getitem__(self, key):
for d in self._dicts:
try:
return d[key]
except KeyError:
pass
raise KeyError, key

def has_key(self, key):
for d in self._dicts:
if key in d:
return True
return False

dict01 = { '1': 'a', '2': 'b' }
for c in range(1, 3):
print "%(c)s %(1)s" % MultiDict(locals(), dict01)
Thanks! This worked great!

Conrad

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 4 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedAug 20, '03 at 5:33p
activeAug 20, '03 at 10:45p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase