Variable arguments: *args and **kwargs
Until now, each function received a fixed number of arguments. But print accepts as many as you want, and so does max(3, 9, 5) …
Take your functions to the next level. In this lesson you will learn how to receive a variable number of arguments with *args and **kwargs, how to pass and return functions, how to create closures with nonlocal, how to write decorators (also with parameters) and how to use functools tools such as reduce, partial and cache.
Until now, each function received a fixed number of arguments. But print accepts as many as you want, and so does max(3, 9, 5) …
In Python functions are objects , just like numbers or texts: they can be stored in variables, passed as arguments and returned…
A decorator is a function that receives another function and returns an improved version of it, without touching its code. It is…
Sometimes you want to customize the decorator: "repeat the function 3 times", "warn only if it takes more than 1 second". For…
The functools module gathers tools for working with functions. These are the three most useful ones.
Create a Python program with a function that adds as many numbers as it receives, using *args .
Create a Python program with a function that receives a variable number of grades and returns several results at once.
Create a Python program with a function that receives any amount of keyword arguments using **kwargs .
Create a Python program with a function that computes a final price and has optional parameters with a default value.
Create a Python program with a function that returns another function .
Create a Python program that uses a closure with nonlocal to have several counters that do not get mixed up.
Create a Python program with your first decorator : a function that wraps another one to add behaviour to it.
Create a Python program with a decorator that keeps track of how many times a function has been called.
Create a Python program with a decorator that receives a parameter: it needs three levels of functions.
Create a Python program that uses functools.reduce to combine all the elements of a list into a single value.