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

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(다른 사용자 한 명의 중간 판 3개는 보이지 않습니다)
7번째 줄: 7번째 줄:
*개수 제한 없이 튜플 형태의 인수를 받아 올 수 있음
*개수 제한 없이 튜플 형태의 인수를 받아 올 수 있음
{{소스헤더|test.py}}
{{소스헤더|test.py}}
<source lang='python'>
<syntaxhighlight lang='python'>
def func(*args):
def func(*args):
     print(args)
     print(args)
15번째 줄: 15번째 줄:
func(1, 2)
func(1, 2)
func(1, 2, 3)
func(1, 2, 3)
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
john@zetawiki$ python test.py
john@zetawiki$ python test.py
(1,)
(1,)
24번째 줄: 24번째 줄:
(1, 2, 3)
(1, 2, 3)
<class 'tuple'>
<class 'tuple'>
</source>
</syntaxhighlight>
:→인수의 개수와 상관없이 튜플 형태로 개수를 받아옴
:→인수의 개수와 상관없이 튜플 형태로 개수를 받아옴


31번째 줄: 31번째 줄:
* <code>*</code>가 2개 사용됨
* <code>*</code>가 2개 사용됨
{{소스헤더|test.py}}
{{소스헤더|test.py}}
<source lang='python'>
<syntaxhighlight lang='python'>
def func(**kwargs):
def func(**kwargs):
     print(kwargs)
     print(kwargs)
39번째 줄: 39번째 줄:
func(a=1, b=2)
func(a=1, b=2)
func(a=1, b=2, c=3)
func(a=1, b=2, c=3)
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
john@zetawiki$ python test.py
john@zetawiki$ python test.py
{'a': 1}
{'a': 1}
48번째 줄: 48번째 줄:
{'a': 1, 'c': 3, 'b': 2}
{'a': 1, 'c': 3, 'b': 2}
<type 'dict'>
<type 'dict'>
</source>
</syntaxhighlight>
:→인수의 개수와 상관없이 딕셔너리 형태로 개수를 받아옴
:→인수의 개수와 상관없이 딕셔너리 형태로 개수를 받아옴



2020년 11월 2일 (월) 02:32 기준 최신판

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