multiline snippets with triple quotes

  • Thread starter Christoph Zwerschke
  • Start date
C

Christoph Zwerschke

I sometimes use triple quotes in order to produce snippets of multiline
code, like that:

if output == html:
snip = '''<html>
<head><title>Hello, World</title></head>
<body bgcolor="aqua"><h1>What's up?</h1>
</html>'''
else:
snip = 'Hello!'

This has the advantage that you don't need to care for inserting newlines
and escaping quotes and is very readable.

However, the triple quoted strings will also contain the indentation of the
Python block. It would not really matter here, since whitespace is ignored
in this case, but there could be situations where whitespace at the
beginning of the lines would matter, and it simply makes no sense to carry
over the identation in most cases.

Of course, I could simply unindent the triple quoted snippets, but then the
Python code starts to become unreadable, if you have many of such snippets
in deeply indented blocks. And isn't bad indentation something that Python
usually forbids "by force" even?

So, what would be the pythonic way to implement such multiline snippets?

Gtx
Chris
 
P

Peter Hansen

Christoph said:
So, what would be the pythonic way to implement such multiline snippets?

Define them externally to that code block. Either put
them above the function, or at the top of the module,
or have them read from an external file, with the
best approach being dependent on the precise situation
at hand. For example, if you have only one or two
such things, just use parentheses and single quotes
instead of the triple quoting, while if you have a
bunch of them but don't want to read them from a file,
put them in a separate module and just import and
reference them as required.

-Peter
 
J

Jeff Shannon

Christoph said:
I sometimes use triple quotes in order to produce snippets of multiline
code, like that:

if output == html:
snip = '''<html>
<head><title>Hello, World</title></head>
<body bgcolor="aqua"><h1>What's up?</h1>
</html>'''
else:
snip = 'Hello!'


[...]

So, what would be the pythonic way to implement such multiline snippets?

IIRC, sequential strings with only whitespace in between them are
automatically concatenated. So the above could be equivalently written as:

if output == html:
snip = "<html>\n"
"<head><title>Hello, World</title></head>\n"
"<body bgcolor="aqua"><h1>What's up?</h1>\n"
"</html>\n"
else:
snip = 'Hello!'


This does have the disadvantage of requiring explicit newlines, however.

Jeff Shannon
Technician/Programmer
Credit International
 
C

Christopher T King

So, what would be the pythonic way to implement such multiline snippets?

The textwrap.dedent() function is provided for just this purpose:

from textwrap import dedent

if output == html:
snip = dedent('''\
<html>
<head><title>Hello, World</title></head>
<body bgcolor="aqua"><h1>What's up?</h1>
</html>''')
else:
snip = 'Hello!'

textwrap.dedent() removes uniform indentation from the beginning of each
line in the string, so you can still insert desired indents in a natural
way. Note the backslash after the opening quote, and how the actual
string starts on the next line: dedent() isn't as smart as pydoc when it
comes to uniformly indenting strings; it doesn't know to skip the first
line (this helps with more general usage, though).
 
G

gohaku

The textwrap.dedent() function is provided for just this purpose:

from textwrap import dedent

if output == html:
snip = dedent('''\
<html>
<head><title>Hello, World</title></head>
<body bgcolor="aqua"><h1>What's up?</h1>
</html>''')
else:
snip = 'Hello!'
Can dedent capture indents or tabs also...
I noticed that if I do the following:
snip = '''<html>blah blah
</html>'''
Tabs are included with snip.

Is there a heredocs (don't know why it's called that) equivalent in
Python?

Thanks in advance.
 
C

Christoph Zwerschke

IIRC, sequential strings with only whitespace in between them are
automatically concatenated.

Yes, but you have to add either brackets around everything or backslashes at
the line ends, otherwise the lines are not kept together.
This does have the disadvantage of requiring explicit newlines, however.

Yes, plus you need to escape the double quotes.

These three disadvantages are why I wanted to use triple quotes.

Chris
 
C

Christoph Zwerschke

The textwrap.dedent() function is provided for just this purpose:

Thanks. That was the thing I was looking for, though it's a big ugly, plus
it eats some unnecessary CPU cycles at run time. But I think one can live
with that.

Chris
 
C

Christopher T King

Can dedent capture indents or tabs also...
I noticed that if I do the following:
snip = '''<html>blah blah
</html>'''
Tabs are included with snip.

dedent() will return that string as is, since the <html> tag is already in
the left-most position. If you do something like this, however:

snip = '''\
<html>blah blah
</html>'''

dedent() will flush the <html> tag left, and the </html> tag will be
indented by one tab (since it has a one-tab indent relative to said:
Is there a heredocs (don't know why it's called that) equivalent in
Python?

AFAIK, triple quotes do everything heredocs do (but with a cleaner
syntax!); the following produce identical output:

bash heredocs:

echo -n <<EOF
Some text.
Woo!
The end.
EOF

Python triple quotes:

print '''\
Some text.
Woo!
The end.'''
 
J

John Lenton

AFAIK, triple quotes do everything heredocs do (but with a cleaner
syntax!); the following produce identical output:

bash heredocs:

echo -n <<EOF
Some text.
Woo!
The end.
EOF

Python triple quotes:

print '''\
Some text.
Woo!
The end.'''

what about
cat <<-EOF
1
2
3
EOF
1
2
3



--
John Lenton ([email protected]) -- Random fortune:
<JHM> Somehow I have more respect for 14 year old Debian developers than
14 year old Certified Microsoft Serfs.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBHemBgPqu395ykGsRAqfKAJ9T/EjsfspD97012cUT++X5kwl7jwCgssxt
UiroR8OI+dNGDF3J1Ap6F/c=
=QuNS
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top