Python Forum

Full Version: interesting gotcha
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
i have been putting comments in the triple-quoted string at the beginning of modules and functions.  this was the source of some errors that did not say what line number.   the error was putting backslash followed by "x[0-9a-f][0-9a-f]" as part of a comment about what backslash-X codes can be decoded by the function.  but since these were in a real string, python was really parsing it all and doing its own backslash-X decoding.  nice that it made me spend a half hour figuring this out because it would not tell me what line number.
Didn't print the line where it founds the error in the error message?
nope, try this extreme simple case that has the issue in python2.  i still need to figure out the minimal case for python3, which works for this small one.

"""\x[0-9a-f][0-9a-f]"""
I did it and it errors me with invalid \x escape. Putting 'r' in front of the doc string eliminates it.
It's normal behaviour. It happens during the parsing of the code before executing it. All after the \ should be a valid escape sequence which can be evaluated.

Or escape the \. r"""string""" as a doc string is bugs me some kind.

But as you said, not any indication where is the error.
i gotta go read up on what all r'' does.
r is raw string literal. It ignores any escapes sequences in the string
is that all it does?
Yes. You can put whatever you want in the raw string and Python will not do any evaluation.

100 bytes ripped from /dev/urandom
>>> print(r"\xf6\xec\r\xb1\x1e\xf6i\x85\x8eo\xe9\x9a\xd7]\xe1AH\xedg\x9f\xa46\xfca\x1bD\xd1\xa4\x14\xe5`\x04T\xcdHD\xc5\xd0\x19\xb4\xe3\xcc~A\xd9\xbe\xe9\x01\xc5\x82\xac\x8e\xca\x95zo\x8cU\xba\x05\x9d\xca\x17/G\xbfUew\xe07\x14,\x9b\x06nw\xea\xd6\xb9\xfbD\xe4\xc4o\x0f\xab\xa6\x8d\xe2\xca\xbd\xc8R\x0e\x13\xe3\xbf\xf3\xbe")

# without r - escaping like crazy :D
>>> print("\xf6\xec\r\xb1\x1e\xf6i\x85\x8eo\xe9\x9a\xd7]\xe1AH\xedg\x9f\xa46\xfca\x1bD\xd1\xa4\x14\xe5`\x04T\xcdHD\xc5\xd0\x19\xb4\xe3\xcc~A\xd9\xbe\xe9\x01\xc5\x82\xac\x8e\xca\x95zo\x8cU\xba\x05\x9d\xca\x17/G\xbfUew\xe07\x14,\x9b\x06nw\xea\xd6\xb9\xfbD\xe4\xc4o\x0f\xab\xa6\x8d\xe2\xca\xbd\xc8R\x0e\x13\xe3\xbf\xf3\xbe")
±öi
Žoé×]áAHígŸ¤6üa                                                            âʽÈRã¿ó¾          Ѥå`TÍHDÅдãÌ~AÙ¾éł¬ŽÊ•zoŒUºÊ/G¿Uewà7,›nwêÖ¹ûDäÄo«¦
>>>
is there any reason not to do this (raw strings) for that first comment string, or any comment string, just as a matter of course everywhere including where it there is no need because there are no backslash sequences at all?

so if you need to assign a string of exactly one backslash to a variable named b, which would you use?
    b = r'\' #1
    b = '\\' #2
    b = '\\'[0] #3
This is not comment string but doc string.

def func()
    """ This is a valid documentation string.
        It's called documentation string not a comment.
        It's not ignored by the Python interpreter.
        It's become a part of the object.
    """
    pass
As you know func.__doc__ contains this doc string. And beeng a string it is evaluated. I prefer to escape the backslash but perhaps depends on the case.

PEP 257:
Quote:For consistency, always use """triple double quotes""" around docstrings. Use r"""raw triple double quotes""" if you use any backslashes in your docstrings. For Unicode docstrings, use u"""Unicode triple-quoted strings""".
Pages: 1 2