Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting python docstrings for dicts

Tags:

What's the recommended way of adding a docstring for a dictionary parameter? I can see multiple line docstring examples here.

I need to document the input arguments to a function in the docstring. If it's a simple variable, I can use something like:

 def func2(a=x, b = y):  """ fun2 takes two integers    Keyword arguments:  a -- refers to age (default 18)  b -- refers to experience (default 0)  """ 

If we have dict passed as input argument to function:

 def func3(**kwargs):      """ takes dictionary as input        <Here how to explain them - Is it like?>        kwargs['key1'] -- takes value1        <or simply>       key1 -- takes value1       """ 
like image 421
webminal.org Avatar asked Nov 03 '14 10:11

webminal.org


People also ask

How do you use docstrings in Python?

Declaring Docstrings: The docstrings are declared using ”'triple single quotes”' or “””triple double quotes””” just below the class, method or function declaration. All functions should have a docstring.

Should I use docstrings in Python?

The docstrings for Python script should document the script's functions and command-line syntax as a usable message. It should serve as a quick reference to all the functions and arguments.

Which command is used to display docstrings of a module?

Python documentation string or commonly known as docstring, is a string literal, and it is used in the class, module, function, or method definition. Docstrings are accessible from the doc attribute (__doc__) for any of the Python objects and also with the built-in help() function.

What is docstrings in Python give an example?

A Python docstring is a string used to document a Python module, class, function or method, so programmers can understand what it does without having to read the details of the implementation. Also, it is a common practice to generate online (html) documentation automatically from docstrings.

What is a doc string in Python?

Python documentation string or commonly known as docstring, is a string literal, and it is used in the class, module, function, or method definition. Docstrings are accessible from the doc attribute (__doc__) for any of the Python objects and also with the built-in help () function.

How to format a specified string using a dictionary in Python?

The str.format () function is used to format a specified string using a dictionary. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics. To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

How do I format a string in Python?

The format () method formats the specified value (s) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {}. Read more about the placeholders in the Placeholder section below. The format () method returns the formatted string. string .format ( value1, value2... ) value1, value2... Required.

What are Python docstrings used for?

Python docstrings As mentioned above, Python docstrings are strings used right after the definition of a function, method, class, or module (like in Example 1). They are used to document our code. We can access these docstrings using the __doc__ attribute.


2 Answers

I generally use the Google docstring style, so a dictionary parameter would look like:

def func(a_dict):     """Some function to do something to a dictionary.      Args:       a_dict (dict of str: int): Some mapping, I guess?      """     ... 

A function that takes **kwargs (note: this is not quite the same as having a dictionary parameter), would look like:

def func(**kwargs):     """Some function to do stuff to arbitrary keyword arguments.      Args:         **kwargs: Arbitrary keyword arguments.      Keyword Args:         <kwarg_name> int: A description         <kwarg_name_2> str: Another description         <...>      """     ... 

If there are specific parameters that should be present (e.g. your key1), they should be separate, not rolled into **kwargs.


In Python 3.x, you can also use function annotations:

def func(a_dict: dict):     """Some function to do something to a dictionary."""     ... 

From Python 3.5, you can be even more explicit using typing:

from typing import Mapping  def func(a_dict: Mapping[str, int]):     """Some function to do something to a dictionary."""     ... 
like image 123
jonrsharpe Avatar answered Oct 24 '22 02:10

jonrsharpe


For those using PyCharm: You can configure your default docstring formats in:

Preferences -> Tools -> Python Integrated Tools -> Docstrings 

as of version 2019 the allowed options are: Plain, Epytext, reStructuredText, NumPy, Google. This functionality will automatically add a docstring skeleton once you've typed three double quotes " and hit enter.

like image 43
Tomasz Bartkowiak Avatar answered Oct 24 '22 02:10

Tomasz Bartkowiak