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

Andrew Barnert abarnert at yahoo.com
Mon Jul 1 04:14:16 CEST 2013


On Jun 30, 2013, at 18:09, Steven D'Aprano <steve at pearwood.info> wrote:

> 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).

The proposal used backticks, not hyphens:

print ```
Now my code looks much better. The output of this
function is as if you had written it in the same way
its written in the code above, except with all newlines
replaced with spaces and big swaths of spaces removed.
```

So, both the ambiguity and a lot of the ugliness you complain about aren't in the proposal at all.

That being said, I don't really like the proposal, and at least half of the alternatives suggested last time this came up a few months ago were better.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130630/a01fe88f/attachment.html>


More information about the Python-ideas mailing list