2017-09-21 1 views
1

함수에 인수로 전달 된 사전 항목에 대한 주석을 제공하는 올바른 방법은 무엇입니까? 당신은 단지 입력 모듈 DICT 클래스를 사용하여 데프 라인에 사전 형식을 지정할 수 있습니다python dictionaries 덧글

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

스핑크스가 요구하는 * 특정 구문을 찾고 계십니까? 그렇지 않으면, 이것이 주로 의견 기반이 아닌 방법을 알지 못합니다. – chepner

답변

-2

: 여기 (스핑크스를 위해 구글 문서 스타일을 기반으로) 내가 만들어 스타일링 아래의 예입니다. 당신은 또한 유형 별칭을 살펴 걸릴 수도 있습니다

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 

(당신은 새로운 파이썬 3 버전을 사용할 수있는 경우) :

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에 대한 별칭을 만들 수 있습니다. –