*Memos:
- My post explains a function.
- My post explains positional-only parameters in function.
- My post explains keyword-only parameters in function.
- My post explains positional-only parameters and keyword-only parameters together in function.
Parameters can be set to a function definition as shown below:
*Memos:
- The basic parameter is called the positional-or-keyword parameter which accepts a positional or keyword argument from a function call. * The doc officially says positional-or-keyword arguments but they're actually positional-or-keyword parameters.
- A parameter can have or doesn't need to have a default value:
- The parameter which has a default value is the required parameter which must get an argument from a function call. *E.g.
def func(p='Hello'): pass
. - The parameter which doesn't have a default value is the optional parameter which can get or doesn't need to get an argument from a function call. *E.g.
def func(p): pass
.
- The parameter which has a default value is the required parameter which must get an argument from a function call. *E.g.
- All the parameters after a required parameter must be required parameters.
- There cannot be multiple same name parameters.
def func(fname, lname, age, gender): pass
def func(fname="John", lname="Smith", age=36, gender="Male"): pass
def func(fname, lname, age=36, gender="Male"): pass
# No error
def func(fname="John", lname="Smith", 36, "Male"): pass
# SyntaxError: invalid syntax
def func(fname, lname, age, gender, lname): pass
# SyntaxError: duplicate argument 'lname' in function definition
Arguments can be set to a function call to be passed to a function as shown below:
*Memos:
- There are a positional argument(
"John"
) and keyword argument(fname="John"
):- A positional argument cares its position to be passed to the corresponding parameter position because it cannot specify a parameter with a parameter name.
- A keyword argument doesn't care its position to be passed to the corresponding parameter position because it can specify a parameter with a parameter name.
- All the arguments after a keyword argument must be keyword arguments.
- There cannot be multiple values for one parameter.
- There cannot be multiple same name keyword arguments.
def func(fname, lname, age, gender):
print(fname, lname, age, gender)
func("John", "Smith", 36, "Male")
func(fname="John", lname="Smith", age=36, gender="Male")
func(**{"fname":"John", "lname":"Smith", "age":36, "gender":"Male"})
func(age=36, lname="Smith", gender="Male", fname="John")
func(**{"age":36, "lname":"Smith", "gender":"Male", "fname":"John"})
func("John", "Smith", age=36, gender="Male")
func("John", "Smith", **{"age":36, "gender":"Male"})
# John Smith 36 Male
func(fname="John", lname="Smith", 36, "Male")
# SyntaxError: positional argument follows keyword argument
func(36, "Smith", age=36, fname="John")
# TypeError: func() got multiple values for argument 'fname'
func(lname="Smith", lname="Brown", age=36, gender="Male")
# SyntaxError: keyword argument repeated: lname
def func(fname="John", lname="Smith", age=36, gender="Male"):
print(fname, lname, age, gender)
func()
# John Smith 36 Male
func("Tom", "Brown")
# Tom Brown 36 Male
func(fname="Anna", gender="Female")
func(**{"fname":"Anna", "gender":"Female"})
func(gender="Female", fname="Anna")
func(**{"gender":"Female", "fname":"Anna"})
# Anna Smith 36 Female
func("Anna", "Brown", age=27, gender="Female")
func("Anna", "Brown", **{"age":27, "gender":"Female"})
func("Anna", "Brown", gender="Female", age=27)
func("Anna", "Brown", **{"gender":"Female", "age":27})
# Anna Brown 27 Female