Types of parameter | Where |
---|---|
Formal parameters | parenthesized in the function definition |
Actual parameters(arguments) | passed during a function call |
Types of Arguments | Where | If need = |
Note |
---|---|---|---|
default arguments | at defining functions | Yes | - become optional and rewritable during the function calls. - function_call(non-default arguments, default arguments) |
keyword arguments | at function call | Yes | - used to passed values through arguments without matching the order of parameters in the function definition - all the keyword arguments should match the parameters in the function definition. |
positional arguments | at function call | No | - values passed through arguments are passed to parameters with matching the order of parameters - function_call(positional arguments, keyword arguments) |
Type of Passing | Note | Example |
---|---|---|
Passing values | - a mix of keyword and positional argument | myfun1(1, 2, 3) myfun1(c = 33, b = 22, a = 11) |
Passing a list | - the element in list is passing as positional arguments | L1 = [11,22,33] myfun1(*L1) |
Passing a dict | - the dict's key should match the formal paramter in the function definition. | d1 = {"c":33, "b":22, "a":11} myfun1(**d1) |
functools | Note | example |
---|---|---|
functools.partial() functools.partialmethod() |
- usefull in optimization - partially pass/override arguments with keyword arguments. |
def objfun1(power, x, y) return (x**power - y)**2 objfun2 = functools.partial(objfun1, x=[1, 2, 3, 4, 5] ) |
functools.reduce() |
- run function over a list, and reduce to one | functools.reduce(lambda x, y: x+y, [1, 2, 3, 4, 5], 20) |