Formatting python docstrings for dicts

大憨熊 提交于 2019-12-05 00:12:14

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.

    """
    ...

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."""
    ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!