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

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 2명의 중간 판 14개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
;<nowiki>*</nowiki>args, <nowiki>**</nowiki>kwargs
;Python 가변길이 매개변수 *args, **kwargs
*<nowiki>*</nowiki>args 의 args는 arguments 의 약어임
*<code>*args</code> 의 args는 arguments 의 약어임
*<nowiki>**</nowiki>kwargs 의 kwargs는 keyword arguments  의 약어임
*<code>**kwargs</code> 의 kwargs는 keyword arguments  의 약어임


==<nowiki>*</nowiki>args==
==*args==
*개수 제한 없이 튜플 형태의 인수를 받아 올 수 있음
*개수 제한 없이 튜플 형태의 인수를 받아 올 수 있음
<source lang='python'>
{{소스헤더|test.py}}
# test.py
<syntaxhighlight lang='python'>
 
def func(*args):
def func(*args):
     print(args)
     print(args)
16번째 줄: 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,)
25번째 줄: 24번째 줄:
(1, 2, 3)
(1, 2, 3)
<class 'tuple'>
<class 'tuple'>
</source>
</syntaxhighlight>
:→인수의 개수와 상관없이 튜플 형태로 개수를 받아옴
:→인수의 개수와 상관없이 튜플 형태로 개수를 받아옴


==<nowiki>**</nowiki>kwargs==
==**kwargs==
*개수 제한없이 딕셔너리 형태의 인수를 받아 올 수 있음
* 개수 제한없이 딕셔너리 형태의 인수를 받아 올 수 있음
*<nowiki>*</nowiki>가 2개 사용됨
* <code>*</code>가 2개 사용됨
<source lang='python'>
{{소스헤더|test.py}}
# test.py
<syntaxhighlight lang='python'>
 
def func(**kwargs):
def func(**kwargs):
     print(kwargs)
     print(kwargs)
41번째 줄: 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}
50번째 줄: 48번째 줄:
{'a': 1, 'c': 3, 'b': 2}
{'a': 1, 'c': 3, 'b': 2}
<type 'dict'>
<type 'dict'>
</source>
</syntaxhighlight>
:→인수의 개수와 상관없이 딕셔너리 형태로 개수를 받아옴
:→인수의 개수와 상관없이 딕셔너리 형태로 개수를 받아옴


==같이 보기==
==같이 보기==
* [[Python 기초]]
* [[Python 기초]]
* [[Python 자료형]]
* [[가변길이 매개변수]]
* [[언어별 가변길이 매개변수]]


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

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