assigning multi-line strings to variables

A

Alf P. Steinbach

I think he repeated it just to let people know that they can get what
they want without following your adsurd advice.

Perhaps you could quote the advice that you find absurd, and explain how you
interpret the quoted text?

My previous experience with you is that you immediately called me "insane" (and
worse) for suggesting a solution that could work in principle and did work in
practice for the problem posed in that thread; I think you resorted to such
characterizations because you had stated that it was impossible.

So until you get a bit more concrete I'm interpreting your characterization
(about what?) as just confirming & uphelding the impression you made back then.


Cheers & hth.,

- Alf
 
S

Steven D'Aprano

That statement should be quantified with "for large chunks of text".
Format string is typically 2-3 lines at most, not enough to qualify as
large chunk.

No it shouldn't. It applies equally for two lines or two hundred lines.

You seem to have missed that these are NOT equivalent:

"""abcd
efgh"""

"abcd"\
"efgh"


To avoid the ugly backslash, you can use Python's implicit line
continuation inside brackets. This is especially handy since you often
use brackets for function calls:

len(
"this is a very long string that won't fit"
" on a single line in the source code, but"
" must be a single line string, which is why"
" a triple-quoted string is not appropriate.")

To get the same result using triple-quoted strings, you would need to do
this:

len(
"""this is a very long string that won't fit\
on a single line in the source code, but\
must be a single line string, which is why\
a triple-quoted string is not appropriate.""")


but the second is risky, because an invisible whitespace character
following the backslash will introduce bugs into your code:
.... """abc\
.... def""") == 6
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
AssertionError
 
L

Lie Ryan

No it shouldn't. It applies equally for two lines or two hundred lines.


You seem to have missed that these are NOT equivalent:

"""abcd
efgh"""

"abcd"\
"efgh"


To avoid the ugly backslash, you can use Python's implicit line
continuation inside brackets. This is especially handy since you often
use brackets for function calls:

You *again* missed the point. The purpose here is to *avoid manual
preprocessing* of the text chunk string concatenation is not suitable
for including large text. Implicit because they force you to preprocess
the chunk before python will accept the text into the source code. If
you added backslashes inside triple-quotes, it's just as ugly as
implicit continuation.

When you don't want the newline, then just let the text flow, there is
no need to *artificially* break the lines, whether using backslash or
implicit concatenation. Triple quoted string is a place where 80-char
limits (or whatever number you set) shouldn't apply; if you insist on
having string inside triple-quotes to be obey 80-chars, then it's just
foolish consistency.

With triple quoting, you just need to copy, paste, then add *exactly
two* backslashes if preserving exact newline is crucial. Compare that
with adding a quote before and after *each line*. That is O(1) vs O(n)
difference.

Large chunk of text inside source code is usually there because you're
too lazy to create a separate file for it. When you're too lazy to even
create a file, you're not likely to be that industrious to do any sort
of preprocessing to the chunk. And you most likely won't care that the
source code looks ugly if it exceeds the oh-so-sacred 80-char limit,
because scripts with that kind of nature (i.e. includes large text chunk
instead of separate file) is typically one-off throwaway scripts. You
want to start working on the chunk immediately instead of spending
ten-minutes forcing python to grok the text.
 
L

Lie Ryan

Perhaps you could quote the advice that you find absurd, and explain how
you interpret the quoted text?

My previous experience with you is that you immediately called me
"insane" (and worse) for suggesting a solution that could work in
principle and did work in practice for the problem posed in that thread;
I think you resorted to such characterizations because you had stated
that it was impossible.

I don't know about your feud with Carl, but for this particular thread,
the problem is that your solution involves much more manual work than is
necessary. Even worse, you suggested to "write a Python script to format
it for you". That is the worse piece of advice I've ever heard.

Yes, your advices works perfectly if you follow it; except that it adds
something to my worklist instead of lifting it.
 
L

Lie Ryan

If you have a 5K string without line breaks, say, then it's entirely
reasonable to write a script to split it up, depending on your fav
editor's lack of functionality for that. Perhaps you'd like to do it
manually. I don't.

Use triple-quoted, let them flow, done. I've never heard of any text
editor in current use without text wrapping capability, even Notepad has
it. And if I've got 5k of text in source code without line breaks I
wouldn't want that silly string to disturb my view of the code. You
argument aren't even convincing.

Perhaps you like to do it the hard way, I don't.
No, you simply haven't thought it through.

I suspect that you have formed some silly idea in your mind about what I
wrote meant, and that that in-your-mind silly idea is what you're
arguing against, for otherwise your comments do not make sense (like,
you state that saving work adds work).

The point is, what you're suggesting doesn't save work at all as you've
shown it. There are other ways to do the same thing, for virtually no
work at all.
 
A

Alf P. Steinbach

Use triple-quoted, let them flow, done. I've never heard of any text
editor in current use without text wrapping capability, even Notepad has
it. And if I've got 5k of text in source code without line breaks I
wouldn't want that silly string to disturb my view of the code. You
argument aren't even convincing.

You'd put a 5K line in your source code, + you're working with text wrapping in
your editor.

OK.


Cheers & hth.,

- Alf
 
N

Neil Cerutti

Use triple-quoted, let them flow, done. I've never heard of any
text editor in current use without text wrapping capability,
even Notepad has it. And if I've got 5k of text in source code
without line breaks I wouldn't want that silly string to
disturb my view of the code. You argument aren't even
convincing.

Perhaps you like to do it the hard way, I don't.

Arguing about how to write 5k of text into your code is about as
sensible as arguing about how to stuff a potato into the tailpipe
of your Chevrolet.
The point is, what you're suggesting doesn't save work at all
as you've shown it. There are other ways to do the same thing,
for virtually no work at all.

Don't put big text dumps in your program. Problem solved!
 
L

Lie Ryan

Arguing about how to write 5k of text into your code is about as
sensible as arguing about how to stuff a potato into the tailpipe
of your Chevrolet.


Don't put big text dumps in your program. Problem solved!

Alf suggested it, not me.
 
L

Lie Ryan

You'd put a 5K line in your source code, + you're working with text
wrapping in your editor.

In the other hand, you'd put a 5K line in your source code, + you're
writing, debugging, and running a script to wrap and put various escapes
for quotes and newlines, + you need to figure out how to force that
script to accept your 5k string.
 
L

Lie Ryan

In the other hand, you'd put a 5K line in your source code, + you're
writing, debugging, and running a script to wrap and put various escapes
for quotes and newlines, + you need to figure out how to force that
script to accept your 5k string.

+ now your chunk is in obfuscated form with various quote noise,
unnecessary escape characters and the like.
 
A

Alf P. Steinbach

+ now your chunk is in obfuscated form with various quote noise,
unnecessary escape characters and the like.

Personally, for Python I'd put such text in a separate text file, as I
recommended first of all in the posting you've reacted so negatively to.


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach

Alf suggested it, not me.

On the contrary, I responded to you on the 30th, using a concrete example of
your triple-quote literals rationale posted on the 29th, that ...

<quote author="Lie Ryan">
Yes, apparently my statement that implicit concatenation is an artifact
is erroneous but it doesn't make the important points less true, that
implicit concatenation is not suitable for integrating large chunk of
text into source code.
</quote

.... but I may have misunderstood what you meant by "large".

Anyway, if you read my first posting in this thread you'll see that the first
thing I suggested is to put the text in a separate text file.

I didn't discuss rationales for choosing this or that method because where it
isn't personal preference it's rather obvious. ;-)


Cheers & hth,

- Alf
 
L

Lie Ryan

Personally, for Python I'd put such text in a separate text file, as I
recommended first of all in the posting you've reacted so negatively to.

Didn't we all already agree that we wouldn't put large chunk of text
directly in source code unless we're too lazy to create a separate file
for a one-off scripts. But your suggested alternative[i.e. quoting
everything, uglily] to the proper way[i.e. creating separate file] is so
distasteful to make it looks like the only clean way of inserting chunk
of text is to create separate file.
 
S

Sibylle Koczian

goldtech said:
Thank you to posters for help to my question. Seems I had trouble with
triple quotes strings in the PythonWin shell. But using the Idle shell
things work as expected. But this is probably another issue...any way,
w/Idle's shell I got the "action" regarding multiline strings I
expected.

What PythonWin version? I remember having had such trouble years ago,
probably Python 1.something.

Greetings
Sibylle
 

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,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top