Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
interesting gotcha
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Didn't print the line where it founds the error in the error message?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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]"""
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
i gotta go read up on what all r'' does.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
r is raw string literal. It ignores any escapes sequences in the string
Recommended Tutorials:
Reply
#7
is that all it does?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
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«¦
>>>
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
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
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
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""".
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  An interesting BUG with double assignment Gribouillis 2 739 Oct-08-2023, 01:56 PM
Last Post: Gribouillis
  most interesting or useful modules? t4keheart 7 3,409 Jan-20-2020, 06:03 AM
Last Post: wavic
  Interesting/Fun Scripts? ejected 2 2,368 Mar-27-2019, 07:49 AM
Last Post: Skaperen
  Interesting article on PyTourch Larz60+ 0 2,277 Jan-03-2018, 09:06 PM
Last Post: Larz60+
  Any interesting works in progress? mpd 5 3,734 Dec-15-2017, 09:34 PM
Last Post: wavic
  An interesting blog wavic 0 2,380 Dec-12-2017, 09:09 PM
Last Post: wavic
  Interesting new features in python3 Kebap 8 5,872 Jul-05-2017, 03:53 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020