Python gotcha of the day

Python python at bladeshadow.org
Thu Mar 15 18:58:46 EDT 2018


On Thu, Mar 15, 2018 at 10:21:24AM +0100, Thomas Jollans wrote:
> On 2018-03-15 07:11, Ben Finney wrote:
> > Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
> > 
> >> py> """\""""""
[...]
> Then riddle me this:
> 
> if """\"""""" is equivalent to "" + "\"" + "" + "", then why isn't
> """ \""" """ equivalent to "" + " \"" + " " + ""?

I've been away on vacation and have deleted a bunch of messages, so
it's likely someone else has already answered this, but if not the
"secret sauce" is that three consecutive quotes are one token, making
the string (or part of it) a triple-quoted string.  That makes the
result of both strings consistent, i.e.:

 """\"""""" => '"'

 - The first three quotes start the triple-quoted string.
 - The quote after the backslash is escaped, and is part of the string
 - The next three quotes end the triple-quoted string
 - the remaining two quotes yeild the empty string

The version with spaces is completely consistent:

""" \""" """ => ' """ '

 - The first three quotes start the triple-quoted string.
 - They're followed by a space, part of the TQS.
 - The quote after the backslash is escaped, and is part of the string
 - The next TWO quotes do not terminate the TQS, because there are
   not three of them consecutively, thus are part of the TQS.
 - the space is clearly part of the TQS.
 - the remaining three consecutive quotes terminate the TQS.

But the real answer, I think, is:  Just Say No to this in real code...
It's extraordinarily unlikely that you'll have a real-world case that
requires it, and it's extremely difficult to read even in you know
well the relevant rules.  If you found yourself tempted to do this,
there's almost certainly a clearer way to write it, either by reducing
the thing to its result, or by doing string addtion, or what have you.




More information about the Python-list mailing list