Reputation:
I'm currently reading python code that is using strings as comments. For example, here's a function
def xyz(x):
"""This is a function that does a thing.
Pretty cool, right?"""
return 0
What's the point of using strings as comments? It seems really strange. The code compiles, but it's very confusing.
Upvotes: 3
Views: 262
Reputation: 37681
"""This is a function that does a thing.
Pretty cool, right?"""
This is called docstring. Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods.
Example
Docstrings can be accessed from the interpreter and from Python programs using the __doc__
attribute:
print(xyz.__doc__)
It outputs:
This is a function that does a thing.
Pretty cool, right?
Another usage:
from pydoc import help
print(help(xyz))
It outputs:
Help on function xyz in module __main__:
xyz(x)
This is a function that does a thing.
Pretty cool, right?
Upvotes: 2
Reputation: 48710
They're called docstrings, and they're used for documenting your code. Tools and builtins like help()
also inspect docstrings, so they're not just for readers of the code, but also users of your code.
Upvotes: 2