2017-09-21 38 views
1

提供作为参数传递给函数的字典项目的注释的正确方法是什么?这是下面一个例子,与定型,我做了(基于在谷歌文档样式狮身人面像):python字典评论

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

通常在这种情况下,你不使用字典,但**使用参数**:'t1','t2'等。这样的事实,参数传递,是*设计*。 –

+0

我有一个不同的类,生成带有温度的格式化字典。如果能够传递整个字典(或者是用户自制的字典),而不是将其分割成单独的组件,然后将每个字典作为参数或类参数。 –

+0

您是否正在寻找Sphinx *所需*的特定语法?否则,我不明白这如何不主要是基于意见的。 – chepner

回答

-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) 
+0

啊,但问题是,字典的关键也是重要的,这是没有描述通过这种方法(除非我失去了一些东西) –

+0

我增加了一点。结帐类型别名和TypeVar。您可以为KnownTemperature创建别名,为ComponentTemperature创建另一个别名。 –