Введение в Python
Теория: Python functions
Полный доступ к материалам
Functions in Python
Common structure
Naming rules
- lower_underscore_case
- letters, numbers, underscores
- leading underscore for private funcs
Why need docstring
- Keep code and docs at the same place
- Useful for IDEs, editors
- Build documentation from code
- help(func) or func.doc prints the doc
- Doctests
What does function return
Function is object
args, kwargs
- def (*args, **kwargs)
- aggregate rest arguments and keyword arguments
- args is a tuple, kwargs is a dict
- useful when you construct arguments step by step
Never use mutable defaults!
Lambdas
- Came from functional programming
- A short expression with only one statement inside
- Pros:
- just-in-place function
- supports argument unpacking
- Cons:
- long ones are ugly
- hard to debug