[Python-ideas] Alternate quote delimiters
Ron Adam
rrr at ronadam.com
Fri May 11 22:54:35 CEST 2007
While watching the various issues about raw strings and quotes being
discussed, I decided look into other ways to resolve string delimiter
collisions that might also improve pythons string handling in general.
And I'd figure I'd let it get shot full of holes here first. ;-)
Here's a nice overview of the issue with various examples.
http://en.wikipedia.org/wiki/String_literal
*Note: The wiki python raw string example has a trailing backslash which
would cause an error.
r"The Windows path is C:\Foo\Bar\Baz\"
In the case of raw strings, the escaped quotes are only needed in order for
tokenize.c to locate the end of the string. The back slashes remain in
the string. The end of the string is the first unescaped quote of the same
type as the starting quote.
>>> s = r"\"Hello World\""
>>> print s
\"Hello World\"
This allows you to enter, back-slash + quote, pairs into a string, but ...
- This doesn't help in entering only quotes. You still need to either use
a different quotes than what is in the string or not use raw strings.
- A minor side effect is that you can't have a raw string end with a single
slash as in the incorrect wiki example above. Other languages which use
escape characters in raw strings have the same issue.
- If you have a long string with multiple quotes as you can in regular
expressions the increased number of \ characters can make the regular
expression more difficult to read and can add up to be more characters than
needed.
- Another minor issue I've found is doctest that do test on quoted strings
may have multiple nested doc strings which may require careful selection of
quote characters. The problem arises because the start and end delimiters
are the same. The start of the nested string ends the top level string.
The current solution is to pick different quote characters or escape quote
characters in the nested strings.
So how about a multiple quoting solution? The examples of this in the wiki
page are of the form...
qq^I said, "Can you hear me?"^
qq at I said, "Can you hear me?"@
qq§I said, "Can you hear me?"§
But that doesn't fit well with pythons use of quotes. But a variation of
this would. Add a 'q' string prefix similar to the 'u' and 'r' prefix's
that takes the first character inside the quotes as an additional
delimiter. Then ending quote will then need to have that same character
proceeding it.
q"^I said, "Can you hear me?"^"
q"""|I said, "Can you hear me?"|"""
The vertical bar is part of the quote here, not part of the string.
rq"^The Windows path is C:\Foo\Bar\Baz\^"
This example will work as expected.
Because the beginning and ending of the strings are not identical, it's
possible that they can allow nesting.
rq"""^
rq"""^
This is a nested string.
^"""
"""
Another nested string.
"""
^"""
The most useful feature of this would be in temporarily commenting out
large blocks of python code. Currently this doesn't work well if the block
that contain triple quoted doc strings.
Another option might be to designate certain quote delimiters for special
purposes.
Dedented strings.
q"""<
This is a
Dedented
Paragraph.
<"""
Commented out source code.
q"""#
def foo(bar):
"""
A bar checker.
"""
return bar is True
#"""
ReST formatted strings.
rq""":
Bullet lists:
- This is item 1
- This is item 2
- Bullets are "-", "*" or "+".
Continuing text must be aligned
after the bullet and whitespace.
Note that a blank line is required
before the first item and after the
last, but is optional between items.
:"""
This use would require some way to preserve it's quoting type so it can
later be used to render the text. (Any ideas?)
Cheers,
Ron
More information about the Python-ideas
mailing list