Functions

Lambda functions

They are small anonymous functions, that can have multiple arguments but only one returned expression.

You can assign them to variables.

lambda arguments : expression

myFunc = lambda a, b : a * b

Currying and Partials

def multiply_by(a: float) -> Callable:
    def multiply(b: float) -> float:
        return a * b;
    return multiply

double: Callable = multiply_by(2)
triple: Callable = multiply_by(3)

double(50)   # -> 100
triple(30)   # -> 90

Take a look at Partials

Default Mutable Values in Parameters

If a parameter is of a type list or any mutable type, and has a default value (like an empty list), calling the function multiples times might produce bugs, because the calls may share the same list object.

def append_to_list(..., list[T] = [])

should be

def append_to_list(..., list[T] = None)

Last updated