2017-09-21 44 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創建另一個別名。 –