Skip to content
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

Closed
quamrana mannequin opened this issue Aug 22, 2017 · 5 comments
Closed

Python fails to parse triple quoted (commented out) code #75436

quamrana mannequin opened this issue Aug 22, 2017 · 5 comments
Labels
build The build process and cross-build interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@quamrana
Copy link
Mannequin

quamrana mannequin commented Aug 22, 2017

BPO 31253
Nosy @bitdancer, @vedgar

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:

assignee = None
closed_at = <Date 2017-08-22.08:24:07.972>
created_at = <Date 2017-08-22.07:26:57.081>
labels = ['interpreter-core', 'build', 'invalid']
title = 'Python fails to parse triple quoted (commented out) code'
updated_at = <Date 2017-08-22.13:19:59.400>
user = 'https://bugs.python.org/quamrana'

bugs.python.org fields:

activity = <Date 2017-08-22.13:19:59.400>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2017-08-22.08:24:07.972>
closer = 'quamrana'
components = ['Interpreter Core']
creation = <Date 2017-08-22.07:26:57.081>
creator = 'quamrana'
dependencies = []
files = []
hgrepos = []
issue_num = 31253
keywords = []
message_count = 5.0
messages = ['300670', '300674', '300675', '300690', '300691']
nosy_count = 3.0
nosy_names = ['r.david.murray', 'veky', 'quamrana']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'compile error'
url = 'https://bugs.python.org/issue31253'
versions = ['Python 3.6']

@quamrana
Copy link
Mannequin Author

quamrana mannequin commented Aug 22, 2017

Python can parse and run this code:

log = list(r'..\Unknown\*.txt')

but not this:

'''
log = list(r'..\Unknown\*.txt')
'''

@quamrana quamrana mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) build The build process and cross-build labels Aug 22, 2017
@vedgar
Copy link
Mannequin

vedgar mannequin commented Aug 22, 2017

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.

@quamrana
Copy link
Mannequin Author

quamrana mannequin commented Aug 22, 2017

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:

r'''
... several lines of code here ...
'''

@quamrana quamrana mannequin closed this as completed Aug 22, 2017
@quamrana quamrana mannequin added the invalid label Aug 22, 2017
@bitdancer
Copy link
Member

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.

@bitdancer bitdancer changed the title Python fails to parse commented out code Python fails to parse triple quoted (commented out) code Aug 22, 2017
@bitdancer
Copy link
Member

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build The build process and cross-build interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

1 participant