default value parameter, non-default value parameter에 대해서

박준영·2021년 5월 17일
0

함수를 정의 할때 default value parameter를 non-default value parameter 앞에 정의 하면 안 되는 이유에 대해서 알아보겠습니다.

def team(first_team="wecode", second_team):
	print(f"{first_team} and {second_team}")
    
 team("weegle")

이 예시 처럼 default값이 non-default 값보다 앞에 위치하게 되면 first_team에 wecode가 지정되어 있는데 team("weegle") 을 통해 first_name에 weegle 이 들어가려 하기 때문에 error가 발생합니다.

def team(first_team, second_team="wecode"):
	print(f"{first_team} and {second_team}")
    
 team("weegle")

이처럼 second team에 wecode를 집어넣어주면 team=("weegle") 이 앞쪽에 위치한 first_team 값에 들어가기 때문에 error가 발생하지 않습니다.

이렇기 때문에 error방지를 위해서 non-default 값을 앞쪽에 위치해줘야합니다.

0개의 댓글

관련 채용 정보

Powered by GraphCDN, the GraphQL CDN