为作为参数传递给函数的字典项提供注释的正确方法是什么。以下是我编写的样式(基于Google Sphinx的文档样式)的示例:
def class_function_w_dict_argument(self, T_in, c_temps):
""" This calculates things with temperatures
Notes:
All temperatures should be given in Kelvin
Args:
T_in (float): Known temperature (K)
c_temps (dict): Dictionary of component temperatures
{T1 (float): temperature (K)
T2 (float): temperature (K)
T3 (float): temperature (K)
T4 (float): temperature (K)
T5 (float): temperature (K)}
Returns:
T_out (float): Calculated temperature
"""
答案 0 :(得分:-2)
您可能只想使用输入模块Dict类在def行中指定字典格式。 (如果你能使用更新的python 3版本)
from typing import Dict
class Foo:
def class_function_w_dict_argument(self, T_in: float, c_temps: Dict[float,float]):
""""
Notes:
All temperatures should be given in Kelvin
Args:
T_in (float): Known temperature (K)
c_temps (Dict[float, float]): Dictionary of component temperatures
Returns:
T_out (float): Calculated temperature
"""
pass
您可能还想查看类型别名:
Temperature = float
TempDict = Dict[Temperature, Temperature]
和TypeVar:
from typing import TypeVar
Temperature = TypeVar('Temperature', float)
some_temperature = Temperature(1.56)