This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2017年08月22日 07:26 by quamrana, last changed 2022年04月11日 14:58 by admin. This issue is now closed.
| Messages (5) | |||
|---|---|---|---|
| msg300670 - (view) | Author: Andrew Wall (quamrana) | Date: 2017年08月22日 07:26 | |
Python can parse and run this code: log = list(r'..\Unknown\*.txt') but not this: ''' log = list(r'..\Unknown\*.txt') ''' |
|||
| msg300674 - (view) | Author: Vedran Čačić (veky) * | Date: 2017年08月22日 08:14 | |
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. |
|||
| msg300675 - (view) | Author: Andrew Wall (quamrana) | Date: 2017年08月22日 08:24 | |
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 ... ''' |
|||
| msg300690 - (view) | Author: R. David Murray (r.david.murray) * (Python committer) | Date: 2017年08月22日 13:17 | |
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. |
|||
| msg300691 - (view) | Author: R. David Murray (r.david.murray) * (Python committer) | Date: 2017年08月22日 13:19 | |
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. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:58:51 | admin | set | github: 75436 |
| 2017年08月22日 13:19:59 | r.david.murray | set | messages: + msg300691 |
| 2017年08月22日 13:17:19 | r.david.murray | set | nosy:
+ r.david.murray messages: + msg300690 title: Python fails to parse commented out code -> Python fails to parse triple quoted (commented out) code |
| 2017年08月22日 08:24:07 | quamrana | set | status: open -> closed resolution: not a bug messages: + msg300675 stage: resolved |
| 2017年08月22日 08:14:21 | veky | set | nosy:
+ veky messages: + msg300674 |
| 2017年08月22日 07:26:57 | quamrana | create | |