[Python-Dev] indented longstrings?

Avi Kivity avi at argo.co.il
Thu Nov 10 10:26:05 CET 2005


Python's longstring facility is very useful, but unhappily breaks 
indentation. I find myself writing code like

    msg = ('From: %s\r\n'

           + 'To: %s\r\n'

           + 'Subject: Host failure report for %s\r\n'

           + 'Date: %s\r\n'

           + '\r\n'

           + '%s\r\n') % (fr, ', '.join(to), host, time.ctime(), err)
    mail.sendmail(fr, to, msg)

instead of

    msg = ('''From: %s
To: %s
Subject: Host failure report for %s
Date: %s

%s
''') % (fr, ', '.join(to), host, time.ctime(), err)
    mail.sendmail(fr, to, msg)


while wishing for a 

    msg = i'''From: %s
              To: %s\r\n'
              Subject: Host failure report for %s
              Date: %s

              %s
              ''' % (fr, ', '.join(to), host, time.ctime(), err)
    mail.sendmail(fr, to, msg.replace('\n', '\r\n'))

isn't it so much prettier?


(((an indented longstring, i''' ... ''' behaves like a regular 
longstring except that indentation on the lines following the beginning 
of the longstring is stripped up to the first character position of the 
longstring on the first line. non-blanks before that character position 
are a syntax error)))

Avi



More information about the Python-Dev mailing list