Пример строки документа
Давайте покажем, как пример многострочной строки документа:
def my_function():
"""Do nothing, but document it.
No, really, it doesn't do anything.
"""
pass
Давайте посмотрим, как это будет выглядеть при печати
print my_function.__doc__
Do nothing, but document it.
No, really, it doesn't do anything.
Объявление строк документации
В следующем файле Python показано объявление строк документации в исходном файле Python:
"""
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"""
Как получить доступ к строке документации
Ниже приведеноинтерактивный сеанс, показывающий, как можно получить доступ к строкам документов
>>> import mymodule
>>> help(mymodule)
Если предположить, что это файл mymodule.py, то эта строка, будучи первым оператором в файле, станет строкой документации модулей mymodule при импорте файла.
>>> help(mymodule.MyClass)
The class's docstring
>>> help(mymodule.MyClass.my_method)
The method's docstring
>>> help(mymodule.my_function)
The function's docstring