string interpolation syntactic sugar

Barry A. Warsaw bwarsaw at cnri.reston.va.us
Thu Dec 9 15:22:25 EST 1999


>>>>> "JH" == Jeremy Hylton <jeremy at cnri.reston.va.us> writes:

    JH> And it's a good technique to point out!  Another variant of
    JH> this that I've found useful is:

    JH>      "a %(x)s b %(y)s" % obj.__dict__

And what I use all the time with executable scripts is something like:

def usage(code, msg=''):
    print __doc__ % globals()
    if msg: print msg
    sys.exit(code)

Then you put all the script's --help text in the module docstring,
with a few %(thingie)s sprinkled around it.  Works really well!

-Barry




More information about the Python-list mailing list