Published on

파이썬에서 기호 *과 **의 의미

Authors
  • avatar
    Name
    Inhwan Cho
    Twitter

Python에서 기호의 의미

파이썬에서 *은 일반적으로 모든 것을 의미합니다

from math import * 이런 식으로 사용 가능하나, 권장하지는 않습니다.

가변인자로서의 *args **kwargs

  • *args(arguments) : list of arguments - as positional arguments
  • **kwargs(keyword arguments) : dictionary -
    whose keys become separate keyword arguments and the values become values of these arguments.

*은 list 또는 tuple **은 dictionary *, ** 모두 함수에 다수의 매개 변수를 허용하기 위해 사용하며 인자의 개수에 제한을 두고 싶지 않을 경우 사용합니다.

만약 딕셔너리를 인자로 넣는 함수를 생성한다면 아래와 같은 방법으로 사용합니다.

def dic_sample(**kwargs):
    print(kwargs)

dic_sample(math=90, english=70, korean=80)

# 출력 결과
{'math': 90, 'english': 70, 'korean': 80}