Writing docstrings - defining function arguments and returning

Suppose I have a function, for example:

>>> def foo(a):
        return a+1

I want to write a documentation line for it.

what is the convention when specifying in a docstring that it takes a and returns a + 1?

+3
source share
4 answers

The idea behind docstring is to give the user a general overview of what happens and goes out, without saying too much about how this happens. In this case:

def foo(a):
    """Take a number a and return its value incremented by 1."""
    return a + 1

For a less trivial example, I like the one found in Dive Into Python for documenting functions:

def build_connection_string(params):
    """Build a connection string from a dictionary of parameters.

    Return string."""

, -. , docstring , ( , ) , ( ).

+7

Python ( ), Python Enhancement .

Python PEP 257 docstrings :

def function(a, b):
"""Do X and return a list."""

:

def function(a, b):
"""function(a, b) -> list"""

.

, PEP , PEP, nitty-gritty docstrings.

, PEP, , Python.

+3
+2

, .

→> help (LEN)

LEN (...)

len(object) -> integer

Return the number of items of a sequence or mapping.
+1
source

Source: https://habr.com/ru/post/1781950/


All Articles