До недавнего времени я думал, что Python Docstrings был просто альтернативным способом написать комментарии в коде. Как я уже узнал, они могут быть весьма полезны в письменной документации прямо в код. Вот что я имею в виду:
# hello.py class Hello: def __init__(self): """This is the Hello class init method. It doesn't really do anything in this code. I just included it here so I can write this long multi-line docstring.""" pass def hello(): 'Simply prints hello world!' print("Hello World!")
Выше это простой сценарий Python – Hello.py
Отказ Чтобы увидеть документацию для этого класса, запустите скрипт в интерактивной оболочке:
$ python3 -i hello.py >>>
Затем введите Помогите (привет)
:
>>> help(Hello)
Аккуратно отформатированная документация для класса должна быть возвращена.
class Hello(builtins.object) | Methods defined here: | | __init__(self) | This is the Hello class init method. It doesn't really | do anything in this code. I just included it here so I can write this | long multi-line docstring. | | hello() | Simply prints hello world! | | ----------------------------------------------------------------------
Оригинал: “https://dev.to/wangonya/documenting-python-code-using-docstrings-1m5o”