-1

How would create a custom docstring in python? Would you just say __nameofdocstring__ or is there something else you should do?

Is it possible to create a new docstring for a certain .py file? I want to write __notes__ = "blah blah blah", but just saying that statement won't work.

1 Answers1

2

Docstring Example

Let's show how an example of a multi-line docstring:

def my_function():
"""Do nothing, but document it.

No, really, it doesn't do anything.
"""
pass

Let's see how this would look like when we print it

print my_function.__doc__

Do nothing, but document it.

    No, really, it doesn't do anything.

Declaration of docstrings

The following Python file shows the declaration of docstrings within a python source file:

"""
Assuming this is file mymodule.py, then this string, being the
first statement in the file, will become the "mymodule" module's
docstring when the file is imported.
"""

class MyClass(object):
    """The class's docstring"""

    def my_method(self):
        """The method's docstring"""

def my_function():
    """The function's docstring"""

How to access the Docstring

The following is an interactive session showing how the docstrings may be accessed

>>> import mymodule
>>> help(mymodule)

Assuming this is file mymodule.py then this string, being the first statement in the file will become the mymodule modules docstring when the file is imported.

>>> help(mymodule.MyClass)
The class's docstring

>>> help(mymodule.MyClass.my_method)
The method's docstring

>>> help(mymodule.my_function)
The function's docstring
Lukas
  • 446
  • 3
  • 11
  • Is that really a proper usage of a function? – DS_Secret Studios Oct 10 '18 at 01:29
  • "Docstring", named for "documentation". That's all. You can document what your functions are doing. – Lukas Oct 10 '18 at 01:30
  • If you're problem is solved please consider marking an answer as correct (whether it be mine, your own or someone else's) so future helper gremlins like yours truly can safely skip the question. Thanks! – Lukas Oct 10 '18 at 01:31