파이썬 가변길이 매개변수 *args, **kwargs

1 개요[ | ]

Python 가변길이 매개변수 *args, **kwargs
  • *args 의 args는 arguments 의 약어임
  • **kwargs 의 kwargs는 keyword arguments 의 약어임

2 *args[ | ]

  • 개수 제한 없이 튜플 형태의 인수를 받아 올 수 있음
test.py
def func(*args):
    print(args)
    print(type(args))

func(1)
func(1, 2)
func(1, 2, 3)
john@zetawiki$ python test.py
(1,)
<class 'tuple'>
(1, 2)
<class 'tuple'>
(1, 2, 3)
<class 'tuple'>
→인수의 개수와 상관없이 튜플 형태로 개수를 받아옴

3 **kwargs[ | ]

  • 개수 제한없이 딕셔너리 형태의 인수를 받아 올 수 있음
  • *가 2개 사용됨
test.py
def func(**kwargs):
    print(kwargs)
    print(type(kwargs))

func(a=1)
func(a=1, b=2)
func(a=1, b=2, c=3)
john@zetawiki$ python test.py
{'a': 1}
<type 'dict'>
{'a': 1, 'b': 2}
<type 'dict'>
{'a': 1, 'c': 3, 'b': 2}
<type 'dict'>
→인수의 개수와 상관없이 딕셔너리 형태로 개수를 받아옴

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}