DEV Community

Nguyễn Đức Tài
Nguyễn Đức Tài

Posted on

A confusion

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

Top comments (3)

Collapse
 
duongdominhchau profile image
Dương Đỗ Minh Châu

""" 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

def f():
    "My fancy function"

def g():
    # My fancy function
    pass

print(f.__doc__)
print(g.__doc__)
Enter fullscreen mode Exit fullscreen mode

The former stores the string and makes it available at runtime, while the comment is discarded and has absolutely no effect at runtime.

def f():
    """My fancy function"""
def g():
    "My fancy function but with docstring written as single-line string"

print(f.__doc__)
print(g.__doc__)
Enter fullscreen mode Exit fullscreen mode

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 """

Collapse
 
claudiogi profile image
ClaudioGi • Edited

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.

import dis
def f(): 
  """nameless triple double quote string""" # will get a name ...
  #  ^-- ... as the first nameless string within a function. 
  1234567890 # nameless integer                 NOP
  12345.6789 # nameless float                   NOP
  "nameless double quote string" #              NOP               
  'nameless single quote string' ##             NOP
  '''nameless triple single quote string''' ##  NOP
  (1,2) # nameless tuple of two integer values  NOP
  return None
dis.dis(f)
print(f' # Available in main code: \n     # {f.__doc__=}')
print(f'     # {dir(f)=}')
print(f'     # {f.__code__=}')
print(f'     # {dir(f.__code__)=}')
print(f'     # {f.__code__.co_code=}')
dis.dis(f.__code__.co_code)
Enter fullscreen mode Exit fullscreen mode

Image description

Collapse
 
sunnyshethbista profile image
Sunny Sheth

use # symbol/sign for comment out the code.