Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the standard way for writing default values in a python docstring?

I have a function with parameters set to default values. I'm using a NumPy-style docstring, but I've seen the default values written elsewhere. What is the commonly accepted placement for writing "default" in the docstring?

def some_func(a_num=None, a_string=None):
    ''' A function that does something special.

    Parameters
    ==========
    a_num : int, default 100                        # is it written here?
        An important number.
    a_string : str, default 'foo'
        A useful string.  Default is 'foo'.         # or here?    
 
    '''
like image 499
pylang Avatar asked Jan 19 '16 17:01

pylang


People also ask

What are default values in Python?

Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.


1 Answers

If you read further in the document you linked it looks like there's no one standard style:

Optional keyword parameters have default values, which are displayed as part of the function signature. They can also be detailed in the description:

Description of parameter `x` (the default is -1, which implies summation
over all axes).

When a parameter can only assume one of a fixed set of values, those values can be listed in braces, with the default appearing first:

order : {'C', 'F', 'A'}
    Description of `order`.

I would recommending picking a style for your own project and sticking to it.

like image 113
Wayne Werner Avatar answered Nov 06 '22 23:11

Wayne Werner