Newbie Variable Substitution Question

Erik Max Francis max at alcyone.com
Wed Aug 20 13:40:09 EDT 2003


ckrieg at shaw.ca wrote:

> 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?

Because when used this way, the string formatting operator % requires
one argument on the left, which is a mapping type.  You're passing it
two.

Consider what would happen if both dictionaries had the same key; which
should be used?  Presuming you wanted them to be checked in order, try:

	d = dict01.copy()
	d.update(locals())
	print ... % d

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ I'm the woman whose / Three wishes came true
\__/  Lamya




More information about the Python-list mailing list