Paul's Python CoursePPC

Docstrings

Not met yet

A string on the first line inside a def, a class or a file is a docstring. Python keeps it as __doc__, and help() shows it, so callers can use a function without reading its body. Say what goes in and what comes back. A string anywhere else in the body is just a string.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks a string anywhere in a function body is its docstring.
  • Thinks a docstring is a comment that Python discards, so the program cannot read it.

Where it comes up