Easy "here documents" ??

John Roth newsgroups at jhrothjr.com
Mon Dec 20 05:17:28 CET 2004

"Jim Hill" <jimhill at swcp.com> wrote in message 
news:cq2pgr$irj$1 at iruka.swcp.com...
> I've done some Googling around on this and it seems like creating a here
> document is a bit tricky with Python.  Trivial via triple-quoted strings
> if there's no need for variable interpolation but requiring a long, long
> formatted arglist via (%s,%s,%s,ad infinitum) if there is.  So my
> question is:
> Is there a way to produce a very long multiline string of output with
> variables' values inserted without having to resort to this wacky
> """v = %s"""%(variable)
> business?

The major issue here is that both ways of text substitution
in Python are based on the C and C++ printf statement.
Here documents are based on a much less disciplined
use of variables; they use the local variables on the
calling stack.

It would certainly be possible to write a module that
substitutes the values of variables in the calling stack.
Look at the inspect module in the Python Runtime
Services section of the Python Library for the basic
facilities you'd need to use to write such a module.

I'm not sure why you'd want to do this, though.
It seems like it would be mostly useful in a style
of programming that's quite foreign to the way
Python wants to be programmed.

John Roth

> Thanks,
> Jim, Python no0b

More information about the Python-list mailing list