*args는 임의의 개수의 Positional Arguments 방식으로, 튜플 형태로 전달
def test(*args):
print(args)
test('a','b','c')
# ('a','b','c')
**kwargs는 임의의 개수의 Keyword Arguments 방식으로, 딕셔너리 형태로 전달
def test(**kwargs):
print(kwargs)
test(a=1,b=2,c=3)
# {'a': 1, 'b': 2, 'c': 3}
해당 그림을 참고하여 아래 Assignment를 해결해보자 !
def func_param_with_var_args(name, *args, age):
print("name=",end=""), print(name)
print("args=",end=""), print(args)
print("age=",end=""), print(age)
func_param_with_var_args("정우성", "01012341234", "seoul", 20)
#아래와 같은 오류 발생
Traceback (most recent call last):
File "/Users/donq/Documents/project/practice_1220.py", line 6, in <module>
func_param_with_var_args("정우성", "01012341234", "seoul", 20)
TypeError: func_param_with_var_args() missing 1 required keyword-only argument: 'age'
name = non-default positional parameter
*args = ariable Length Positional Parameter
age = non-default keyword parameter
def func_param_with_var_args(name, *args, age):
print("name=",end=""), print(name)
print("args=",end=""), print(args)
print("age=",end=""), print(age)
func_param_with_var_args("정우성", "01012341234", "seoul", age = 20)
def func_param_with_var_args(name, age, *args):
print("name=",end=""), print(name)
print("args=",end=""), print(args)
print("age=",end=""), print(age)
func_param_with_var_args("정우성", 20, "01012341234", "seoul")
name=정우성
args=('01012341234', 'seoul')
age=20
def func_param_with_kwargs(name, age, **kwargs, address=0):
print("name=",end=""), print(name)
print("age=",end=""), print(age)
print("kwargs=",end=""), print(kwargs)
print("address=",end=""), print(address)
func_param_with_kwargs("정우성", "20", mobile="01012341234", address="seoul")
#아래와 같은 오류 발생
invalid syntax
name = non-dafault positional
age = non-dafault positional
**kwargs = variable Length Keyword
address = default keyword
1. 파라미터의 배치가 잘 못 되어 있다.
def func_param_with_kwargs(name, age, address=0, **kwargs):
print("name=",end=""), print(name)
print("age=",end=""), print(age)
print("kwargs=",end=""), print(kwargs)
print("address=",end=""), print(address)
func_param_with_kwargs("정우성", "20", mobile="01012341234", address="seoul")
name=정우성
age=20
kwargs={'mobile': '01012341234'}
address=seoul
def mixed_params(name="아이유", *args, age, **kwargs, address):
print("name=",end=""), print(name)
print("args=",end=""), print(args)
print("age=",end=""), print(age)
print("kwargs=",end=""), print(kwargs)
print("address=",end=""), print(address)
mixed_params(20, "정우성", "01012341234", "male" ,mobile="01012341234", address="seoul")
#아래와 같은 오류 발생
invalid syntax
def mixed_params(age, name="아이유", *args, address, **kwargs):
print("name=",end=""), print(name)
print("args=",end=""), print(args)
print("age=",end=""), print(age)
print("kwargs=",end=""), print(kwargs)
print("address=",end=""), print(address)
mixed_params(20, "정우성", "01012341234", "male" ,mobile="01012341234", address="seoul")
name=정우성
args=('01012341234', 'male')
age=20
kwargs={'mobile': '01012341234'}
address=seoul