[Python-ideas] Idea for new multi-line triple quote literal

chris priest nbvfour at gmail.com
Mon Jul 1 21:34:13 CEST 2013


If you want specific indentation, then use the regular triple quote and 
write it exactly how you want it.
On the other hand, the 99% use case (in my experience at least) you don't 
care about eh indentation.
All you care about is the words. This is the usecase for triple backtick.

The most common example is exceptions. When I raise exceptions, I like to 
include long, verbose error messages. Right now I have three options:

raise TypeError(Live with a really long string going past the 80 char 
limit, thus breaking PEP8 and being annoying")

or

raise TypeError("""
   Use a triple string
   But then live with extra newlines
   and indentation.
   The suggestion to have a dedent
   method makes things a little better
   But imo still not ideal.
""".strip().dedent().replace("/n', ' '))

raise TypeError("Another Option is to "
   "do it like this, but if you do it this way "
   "you run the risk of forgetting a trailing"
   "space at the end of each line and other errors."
   "Not to mention it's annoying when you want to "
   "add words because you have to rewrap and its "
   "generally really annoying to have to deal with")

raise TypeError(```
   The best solution is a new type of
   string literal that just deals with
   the text so I don't have to worry about
   a thing. This is only meant for text where I
   don't need explicit indenting and/or whitespace
   requirements.
```)

On Monday, July 1, 2013 11:12:30 AM UTC-7, Ron Adam wrote:
>
>
>
> On 07/01/2013 12:08 PM, nbv4 wrote: 
> > The only problem I see with explicitly passing in the number of 
> characters 
> > to the dedent function is that you couple your code with th source of 
> that 
> > code. What happens when you copy+paste that function to a class where 
> the 
> > indention level does not match. You then will have to change that 
> number, 
> > or else your code will break. Also, if you run your code through pylent 
> or 
> > something and it changes your indenting from tabs to spaces. 
>
> The number isn't how much to dedent.  But how much white space each 
> non-blank line should have at the beginning. 
>
>        s.margin(0)      # remove common leading space like dedent(). 
>
>        s.margin(4)      # specifies it to have 4 spaces. 
>
> No matter how much (or little) it's indented in your code, it will still 
> be 
> exactly and explicitly what you specify in the string... 
>
>
> s1 = """ 
>      This string is indented 
>      4 spaces in the source code, 
>      but will have a margin of 
>      24 spaces when printed.""".margin(24) 
>
>
>          s2 = """ 
>              This string is indented 
>              12 spaces in the source code, 
>              but will have a margin of 
>              4 spaces when it's printed.""".margin(4) 
>
>
> Use .margin(0), and it removes all the common leading white space just 
> like 
> dedent.   (Makes dedent redundant) 
>
>
> Lets say you want to use a string in different places.  For example on the 
> screen you might want it to be 4 spaces, and on a printed out log you 
> would 
> like it to be 8 spaces.  And in your source code it is indented 12 spaces. 
>
>          s3 = \ 
>              """ 
>              This is an output string I'm 
>              going to use in several places with different margins. 
>              """ 
>
>          print(s3.margin(4))    # to the screen with a margin of 4. 
>          print(s3.margin(8), file=logfile)    # with a margin of 8. 
>
> Notice that the indention level of the initial string doesn't matter. 
>
> Cheers, 
>     Ron 
>
>
>
>
>
>
>
>
>
>
>
>
> What you are thinking of is a relative indent/dedent method.  That would 
> be 
> completely different.  You would have to specify how much to remove or 
> add, 
> and each time you use it on the string it would remove or add that amount 
> again.  So no, that isn't what I'm suggesting. 
>
> Cheers, 
>     Ron 
>
>
> _______________________________________________ 
> Python-ideas mailing list 
> Python... at python.org <javascript:> 
> http://mail.python.org/mailman/listinfo/python-ideas 
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130701/16360715/attachment-0001.html>


More information about the Python-ideas mailing list