Newbie Variable Substitution Question

ckrieg at shaw.ca ckrieg at shaw.ca
Wed Aug 20 13:33:16 EDT 2003


Just a quick question.  Consider this:

dict01 = { '1': 'a', '2': 'b' }

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

1 a
2 a

But, using this loop instead:
    
for c in range(1, 3):
    print "%(c)s %(1)s" % (locals(), dict01)
    
Results in an error: TypeError: format requires a mapping

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




More information about the Python-list mailing list