Chapter 12 of 15All chapters
Chapter 12 of 15
Functions
def, arguments, return values and lambda.
Defining and calling
def names a function and its parameters, and return sends a value back. A function with no return gives None.
- Defaults make an argument optional: def greet(name, greeting="Hello").
- Never use a list or dict as a default. It is created once and shared between calls.
Arguments and lambda
Arguments can be passed by position or by name, and *args and **kwargs collect the extras. lambda makes a small anonymous function for places like a sort key, and anything longer deserves a def.