string interpolation syntactic sugar
Jeremy Hylton
jeremy at cnri.reston.va.us
Thu Dec 9 12:26:31 EST 1999
>>>>> "SM" == Skip Montanaro <skip at mojam.com> writes:
SM> It's perhaps worth pointing out that in situations where all the
SM> items you want to interpolate are local variables you can use
SM> the locals() builtin function instead of building a dict
SM> on-the-fly in your code
SM> "a %(x)s b %(y)s" % locals()
SM> (I know Jeremy knows this, just pointing it out for others new
SM> to Python's string interpolation facility.)
And it's a good technique to point out! Another variant of this that
I've found useful is:
"a %(x)s b %(y)s" % obj.__dict__
Jeremy
More information about the Python-list
mailing list