Functions Reference
Scope boundaries, closures, *args, and keyword argument bindings.
Allow a function to accept any number of positional arguments.
Used In: Dynamic pipeline logging, wrapping commands.
def func(*args):def log_steps(*args):
for step in args:
print(step)
log_steps('Extract', 'Transform', 'Load')Extract
Transform
LoadTime Complexity: O(1) capture, O(N) access
Related Methods: **kwargs
*args (positional arguments as tuple) vs **kwargs (keyword arguments as dictionary).
Allow a function to accept any number of keyword-value arguments.
Used In: Orchestration config initialization, db parameters mapping.
def func(**kwargs):def make_config(**kwargs):
print(kwargs.get('port', 5432))
make_config(host='localhost', port=8000)8000Time Complexity: O(1) capture, O(1) lookups
Related Methods: *args
Demonstrate the mutable default parameters pitfall and show the correct replacement pattern.
Used In: Creating secure, side-effect-free function APIs.
def func(val, lst=None):def append_val(val, lst=None):
if lst is None:
lst = []
lst.append(val)
return lst
print(append_val(1))[1]Time Complexity: O(1)
Common Mistakes: Writing def append(val, lst=[]).
Enforce callers to specify parameters by keyword names.
Used In: Enforcing code clarity in production APIs.
def func(a, *, key_only):def run_etl(source, *, dry_run=False):
print(dry_run)
run_etl('db.csv', dry_run=True)TrueTime Complexity: O(1)
Enforce callers to specify parameters solely by positional sequence.
Used In: Standard library interfaces optimization.
def func(pos_only, /, other):def double(x, /):
return x * 2
print(double(5))10Time Complexity: O(1)
Unpack elements from collections into positional arguments using asterisk.
Used In: Passing dynamically parsed database connection settings.
func(*list, **dict)def add(x, y):
return x + y
params = [10, 20]
print(add(*params))30Time Complexity: O(1)
Decorate parameter and return variables with metadata types hints.
Used In: Static check pipelines, IDE autocomplete settings.
def func(x: int) -> str:def clean(text: str) -> str:
return text.strip()
print(clean(' data '))'data'Time Complexity: O(1)
Related Methods: typing module
Document function scope behavior and parameters in standard markdown strings.
Used In: Module level code self-documentation.
def func():
"""Docstring description"""def process():
"""Cleans data."""
pass
print(process.__doc__)'Cleans data.'Time Complexity: O(1)