New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python fails to parse triple quoted (commented out) code #75436
Comments
Python can parse and run this code: log = list(r'..\Unknown\*.txt') but not this: ''' |
That's because it is not commented code, it is a multiline string literal. And it is not raw, so \Un... is just an error, since n is not a valid hexadecimal digit. In the first code, \Un... is inside a raw string so it is read literally. There is nothing to fix here. If you really want to continue confusing string literals and comments, you'll have to use r'''...''' to be a bit more general for this case. |
My lazy way of programming is to piece together lines of code, going round and round commenting and uncommenting sections until I get something that works. My new lazy way of commenting out larger chunks of code will be:
|
Just FYI, Vedran, almost everyone gets this one wrong :) I too once thought that triple quoted text used as comments was bad style, but in fact I learned they are an accepted way in Python to do multiline comments. Accepted by Guido, at least: https://sgillies.net/2017/05/30/python-multi-line-comments-and-triple-quoted-strings.html :) It is not a common practice, though, in my observation, since most code editors support automatically prefixing and unprefixing a block with '#' characters, and highlight such blocks as comments while they do not highlight strings used as comments as comments. It is an interesting observation that to use it to comment out a block of code one should use the raw string version. Hopefully the existence of this issue will make that slightly more discoverable. |
And being "accepted" does not change the fact that one needs to be aware of the fact that syntactically they are string literals and not syntactic comments. Which was your point. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: