I have just learned more few things about add comments in Python, but somehow I can’t understand what situation we can use #
or use ”””___”””
. It’s actually the same so I very confused between using these
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (3)
"""
is just multiline string, it is not really comment, only#
is comment. Your confusion may come from the fact that other languages extract documentation from comment but Python stores it on the class/function at runtime.Anywhere a string can be used, you can use
"""
.These 2 functions are different
The former stores the string and makes it available at runtime, while the comment is discarded and has absolutely no effect at runtime.
The first string literal inside class/function is considered docstring, there is nothing special about
"""
, it's just being more convenient writing multi-line string with"""
so docstring are usually written with"""
In Python you can deliberately spread nameless values over your code. See the code below and its output showing how the lines of code compile to Python bytecode. You can see that the nameless values won't be processed (NOP no operation).
If your code editor supports full range of Python code syntax highlighting you can "misuse" this Python functionality to bring a bit of color into the documentation of your code. The attached image shows the code as it is shown in SciTE where I have choose different text style for # comments ## comments and for the four kinds of string literals.
See here how you can combine comments and triple quoted strings to quickly switch between two sections of source code while testing the code.
use
#
symbol/sign for comment out the code.