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

Ron Adam ron3200 at gmail.com
Mon Jul 1 20:12:30 CEST 2013



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




More information about the Python-ideas mailing list