Functions Flashcards
Arguments
positional *args
keyword **kwargs
variable-length (varargs): *args, **kwargs
required
optional
actual parameters
Parameters
(pos, /, pos_or_kw, *, kw)
pos and **kwargs can have same names
PEP 570
formal parameters
Parameters order
non-default parameters, default parameters
Arguments order
positional, keyword
Lambda
bound variable
higher-order
single expression
no type annotations
Global Scope
declared in the main body
available from any scope
global variable
global keyword
namespace of __main__ module
module scope
Local Scope
declare inside function
available within the function
Nonlocal Variables
nonlocal keyword
used in nested functions
variable belongs to the outer function
variable refers to the previously bound variable in the closest enclosing scope
Enclosing Scope
scope of the outer function
local scope of any enclosing function’s local scopes
nonlocal scope
Built-in Scope
special reserved keywords
LEGB
Local
Enclosing
Global
Built-in
First-Class Function
- assign to variable
- pass as an argument
- return from function
first-class citizen
Higher-Order Function
a function that can take other functions as arguments or return functions as results
map, filter
Free Variable
variable that is used in a code block but not defined there
Closure
function that retains access to variables from the enclosing scope even after the outer function has finished executing
closure factory