파이썬 위치 인자 키워드 인자 모으기(*)
2023.11.22 22:09
함수를 사용할 때 위치 인자와 키워드 인자를 사용한다.
위치 인자를 모을 때는 *(애스터리스크)를 사용한다.
매개변수들을 튜플로 묶어 준다.
def position(*args):
print(args)
position(1, 2, 3, 'hi')
결과는 (1, 2, 3, 'hi')
가변인자를 사용할 경우 마지막에 *args를 사용해서 나머지 인자를 묶어 준다.
def position(arg1, arg2, *args):
print(arg1, arg2, args)
position(1, 2, 'hello', 'hi)
결과는 1 2 ('hello', 'hi')
키워드 인자를 모을 때는 **를 사용한다.
키워드 인자를 딕셔너리로 묶어 준다.
def keyword(**kwargs):
print(kwargs)
keyword(first='hello', second='hi', third='haha')
결과는 {first: 'hello', second: 'hi', third: 'haha'}