"파이썬 가변길이 매개변수 *args, **kwargs"의 두 판 사이의 차이

27번째 줄: 27번째 줄:
</source>
</source>
:→인수의 개수와 상관없이 튜플 형태로 개수를 받아옴
:→인수의 개수와 상관없이 튜플 형태로 개수를 받아옴
==<nowiki>**</nowiki>kwargs==
*개수 제한 없이 딕셔너리 형태의 인수를 받아 올 수 있음
*<nowiki>*</nowiki>가 2개 사용됨
<source lang='python'>
# 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)
</source>
<source lang='console'>
john@zetawiki$ python test.py
{'a': 1}
<type 'dict'>
{'a': 1, 'b': 2}
<type 'dict'>
{'a': 1, 'c': 3, 'b': 2}
<type 'dict'>
</source>
:→인수의 개수와 상관없이 딕셔너리 형태로 개수를 받아옴
==같이 보기==
==같이 보기==
* [[Python 기초]]
* [[Python 기초]]


[[분류: Python]]
[[분류: Python]]

2017년 12월 4일 (월) 14:37 판

1 개요

*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 }}