[Python!] -Function Parameters

Hailey Park·2021년 11월 4일
0

Python!

목록 보기
1/11
post-thumbnail

1. The order of default value parameter

def func_param_with_var_args(age = "29", name):
    print("name=",end=""), print(name)
    print("age=",end=""), print(age)

func_param_with_var_args("Hailey")

If we code as above, we'll see the following error message.

SyntaxError: non-default argument follows default argument

It's because the default value parameter only assign the default value when there's no parameter assigned. In the above code, the parameter age could be assigned as "Hailey", so then there's no name value exist which causes error.

2. The order of positional arguments and variable length arguments

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("Hailey", "010111111111", "seoul", 29)

If we code as above, we could face the below error message.

TypeError: func_param_with_var_args() missing 1 required keyword-only argument: 'age'

It's because "010111111111", "seoul, and 29 are assigned to the variable length argument args, so there's no value for the positional argumentage.

We could then fix the code as below.

def func_param_with_var_args(name, age, *args):
    print("name=",end=""), print(name)
    print("age=",end=""), print(age)
    print("args=",end=""), print(args)

func_param_with_var_args("Hailey", "010111111111", "seoul", 29)

print(func_param_with_var_args)

Result :

name=Hailey
age=010111111111
args=('seoul', 29)

3. The position of variable length keyword arguments

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("Hailey", "29", mobile="01012341234", address="seoul")

If we code as above, we could see the following error message.

SyntaxError: invalid syntax

It's because both "mobile" and address are assigned to the variable keyword argument kwarg . So there's no value for the keyword argument address.

We could fix it as below.

def func_param_with_kwargs(name, age, address=0, **kwargs):
    print("name=",end=""), print(name)
    print("age=",end=""), print(age)
    print("address=",end=""), print(address)
    print("kwargs=",end=""), print(kwargs)

func_param_with_kwargs("Hailey", "29", mobile="01012341234", address="seoul")

4. The position of positional arguments and variable length keyword arguments

def mixed_params(name="Hailey", *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", "female" ,mobile="01012341234", address="seoul")

If we code as above, then we'll see the error message as below.

SyntaxError: invalid syntax

It's because the order of the parameter and arguments is as the below image for the same reasons as the other errors above.


Resource : https://getkt.com/blog/python-keyword-only-arguments/

So then we can fix the above code as followings:

def mixed_params(age, address1, name="Hailey", *args, **kwargs):
    print("name=",end=""), print(name)
    print("args=",end=""), print(args)
    print("age=",end=""), print(age)
    print("kwargs=",end=""), print(kwargs)
    print("address1=",end=""), print(address1)

mixed_params(20, "seoul", "Hailey", "01012341234", "female" ,mobile="01012341234", address2="seoul")

Result:

name=Hailey
args=('01012341234', 'female')
age=20
kwargs={'mobile': '01012341234', 'address2': 'seoul'}
address1=seoul
profile
I'm a deeply superficial person.

0개의 댓글