[Python] Edit > Block Comment doesn't use triple double-quotes
-
Hell,
After selecting a block of code and hitting Edit > Comment/Uncomment > Block Comment, I expected N++ to use the “triple double-quote” syntax, but instead, if prepends each line with a pound sign:
""" some block comment """
Do I need to edit N++'s configuration?
Thank you.
-
@Shohreh said in [Python] Edit > Block Comment doesn't use triple double-quotes:
Do I need to edit N++'s configuration?
Right now, there is no way in native Notepad++ to change a configuration for what gets used for the block comment action for a given language.(And I don’t know of a plugin that will; I cannot even think of a way with the PythonScript or other scripting plugin to change the native behavior. Sorry. Obviously, with the PythonScript plugin, one could define a separate command, and give it a keyboard shortcut, to insert the “”" before and after the selected text, which might be sufficient for you)And technically, that syntax is “docstring” syntax in Python, not actually a comment, though many people use it as such. But I have run across when trying to put psuedo-unicode escapes \uVWXYZ that go outside of the range that it likes, and when I put it in a docstring instead of a comment, the python parser complained about invalid unicode character, because it’s actually interpreting that section as a string internally, rather than just ignoring commented text, so I had to change from using a docstring to using individual comments. Thus, my “and technically” isn’t just me being pedantic; it has real implications that have influenced me and made me change my code in the past:
c:> python Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> # \u+2611other >>> """ ... \u+2611other ... """ File "<stdin>", line 1 """ ^^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 1-2: truncated \uXXXX escape >>>
-
@Shohreh ,
Right now, there is no way in native Notepad++ to change a configuration for what gets used for the block comment action for a given language.Well, I was wrong (hence, I crossed it out). I started peeking into the source code to see how block comments were implemented, and got a hint that led me to a solution:
Following the User Manual’s advice for editing Notepad++ config files, edit
%AppData%\Notepad++\langs.xml
: go to the<Language name="python" ...
line; after thecommentLine="#"
in that line, addcommentStart=""""" commentEnd="""""
and save; exit Notepad++ and restart, and now when you use Block Comment, it will use the triple-quote as the start and end.I had forgotten that was defined in the
langs.xml
file. Sorry for the original misinformation.—
update: D’OH! It’s even implied in the User Manual already, so I really should have known/remembered it. :-(I will, however, clarify the UM to make it 100% obvious which attributes apply to which.
_update 2: coming soon to a User Manual near you:
-
In addition to what @PeterJones said:
- triple-quoted strings are NOT comments (this is just to reinforce what Peter already said, for emphasis!)
- if you ever have any
\
in the line(s) you are trying to use this method of “commenting” on, you will very likely run into trouble (Python 3 will give you errors when you run your code) – you’d have better luck with a raw triple-quoted string, i.e.,r"""I am a comment"""
- indentation must be correct for this – you can’t, in a block of code that is indented, start one of these ‘comments’ at an arbitrary indentation level – it must match the indentation level of the surrounding lines of code
-
@Alan-Kilborn Thank you. Too bad because I like it better to comment a block.
-
@Shohreh said in [Python] Edit > Block Comment doesn't use triple double-quotes:
I like it better to comment a block.
Well, that’s fine; you can certainly make use of it.
My main point was to point out further caveats for its usage (in addition to what Peter said).BTW, I tried Peter’s advice for changing
langs.xml
, and the advice is sound as to the “how to”, but the result for Python is lacking (not Peter’s fault)… -
@Shohreh said in [Python] Edit > Block Comment doesn't use triple double-quotes:
Too bad because I like it better to comment a block.
If you want, you can use the docstring for pseudo-comments – many python programmers do – but you just have to understand that the Python interpreter treats them differently than a
#
comment, so you have to maintain valid string syntax in any block of text that you put between"""
markers; if you are willing to accept that limitation, then feel free to use docstrings for comments – and feel free to update thelangs.xml
to allow you to use the block-comment feature in Notepad++ to do that wrapping.