Python Function Parameters

77raina·2020년 4월 22일
0

Function gets the value from input parameters and returns the output value, so when we call the function, we pass in the parameter to the function.

Default parameter


It is a basic format of function parameter that goes in order. One weakness is if we mistakenly switch the value with another value, it cannot be easily noticed.

Keyword Arguments

Instead of passing in the values in order, there is another way around not to get mixed up by assigning the value that matches the parameter name.

It has lower possiblities to mistakenly switch the value with another value, and reader can definitely see which value is which by its parameter value.

Mixing positional arguments and keyword arguments

We can mix positional arguments and keyword arguments, but the important thing is we should keep in mind the order of the positional arguments.

Parameter Default Value

Default value can be assigned in the function parameter, so no value needs to be passed in. Then default value will be automatically passed in.
We should keep in mind that if non-default value parameter comes before default value parameter, it'll raise a syntax error.

def love_you(my_name = "정우성", your_name):
       print(f"{my_name} loves {your_name}")

In this case, it throws a syntax error with a message, 'non-default argument follows default argument'.

  • Overview of the paramter order
    As shown above, this is what python function interpreter wants parameter to be arranged.
  • If positional arguments and keyword only arguments are mixed up and variable length positional arguments try to suck up leftovers, it'll take any erroneous positional arguments as well.
  • Also, some unexpected non compatible type might cause a syntax error.
  • If keyword-only argument is not passed in the function call, it throws TypeError exception.
profile
Backend Developer

0개의 댓글