[Python-ideas] Idea for new multi-line triple quote literal
Steven D'Aprano
steve at pearwood.info
Mon Jul 1 03:09:23 CEST 2013
Hi nbv4, and welcome.
On 01/07/13 10:32, nbv4 wrote:
> The tripple quote string literal is a great feature, but there is one
> problem. When you use them, it forces you to break out of you're current
> indentation which maks code look ugly. I propose a new way to define a
> triple back quote that woks the same way regular triple quotes work, but
> instead does some simple parsing of the data within the quotes to preserve
> the flow of the code. Due to the brittle and sometimes ambigious nature of
> anything 'automatic', this feature is obviously not meant for data where
> exact white space is needed. It would be great for docstrings, exception
> messages and other type text.
>
> Here is a short example of it's usage:
> https://gist.github.com/priestc/5897602
For something as trivial as the example you give, there is no need to send people off to a website, which they may not have access too. Here's the simplified version:
# Proposed syntax
def func():
s = """line 1
line 2
line 3"""
t = ---line 1
line 2
line 3---
return s, t
The difference being, lines 2 and 3 of s will begin with four spaces, while t reduces the whitespace between lines to a single space:
s == 'line 1\n line 2\n line 3'
t == 'line 1 line 2 line 3'
I don't think this is particularly useful. I would be more interested in it if it kept the newlines but got rid of the leading spaces:
t == 'line 1\nline 2\nline 3'
but in either case, I think the choice of --- as delimiter is ugly and arbitrary, and very likely is ambiguous (currently, x = ---1 is legal code). Similar suggestions to this have been made many times before, you should search the archives:
http://mail.python.org/mailman/listinfo/python-ideas
Regards,
--
Steven
More information about the Python-ideas
mailing list