Python Functions

Args, kwargs, lambdas, and signatures that document themselves.

  • python functions
  • args kwargs
  • lambda
  • lru_cache
  • type hints

Python Functions

Args, kwargs, lambdas, and signatures that document themselves.

Commands

CommandWhat it does
def f(name, *, verbose=False):Keyword-only arg after *
def f(*args, **kwargs):Catch-all parameters
lambda x: x * 2Tiny anonymous function
functools.lru_cacheMemoize pure functions
def f(path: str) -> int:Type-annotated signature

Example

from functools import lru_cache
@lru_cache(maxsize=1024)
def fib(n: int) -> int:
    return n if n < 2 else fib(n-1) + fib(n-2)

FAQ

**When is *args/kwargs right?

Decorators and pass-through wrappers; concrete signatures are better for public APIs.

Do type hints run at runtime?

No — they are metadata for mypy/IDEs; use pydantic or asserts for runtime validation.

常見問題

When is *args/**kwargs right?
Decorators and pass-through wrappers; concrete signatures are better for public APIs.
Do type hints run at runtime?
No — they are metadata for mypy/IDEs; use pydantic or asserts for runtime validation.

Python