def name(last_name, first_name):
print(f"{last_name}{first_name}")
name("홍", "길동") # 홍길동
def name(last_name, first_name):
print(f"{last_name}{first_name}")
name(first_name = "길동", last_name = "홍")
def name(last_name, first_name):
print(f"{last_name}{first_name}")
name(first_name = "길동", "홍") # SyntaxError: positional argument follows keyword argument
def name(last_name, first_name="길동"):
print(f"{last_name}{first_name}")
name("홍") # 홍길동
👉 함수를 정의 할때 default value parameter를 non-default value parameter 앞에 정의 하면 안 되는 이유
def name(last_name = "길동", first_name):
print(f"{last_name}{first_name}")
name("홍") #SyntaxError: non-default argument follows default argument
- Regular Positional Arguments
- Default Arguments
- Variable Length Positional Arguments
- Non-Default Keyword-Only Arguments
- Keyword-Only Arguments
- Keyword-Only Arguments With Defaults
- Variable Length Keyword Arguments
✅ 지금까지 공부한 내용과 parameter 우선순위를 고려하여 에러가 발생할 수 있는 몇 가지 경우의 수와 해결책을 알아보도록 하자.
👉 위치 인수(positional arguments)와 가변 인수(variable length arguments)의 위치에 따른 output 값
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)
#TypeError: func_param_with_var_args() missing 1 required keyword-only argument: 'age'
# parameter의 순서 변경하기
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
# age 값으로 전달하고 싶은 인자 값(20)을 keyword argument로 전달하기
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)
# name=정우성
# args=('01012341234', 'seoul')
# age=20
👉 가변 키워드 인수 (variable length keyword arguments)의 위치에 따른 output 값
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")
#SyntaxError: invalid syntax
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=정우성
# args=('01012341234', 'seoul')
# age=20
👉 위치 인수와 키워드 가변 키워드 인수 (variable length keyword arguments)의 위치에 따른 output 값
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")
#SyntaxError: 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