🐍 Python Intermediate

What is Python's functools module?

Why Interviewers Ask This

This question targets practical, hands-on experience with Python. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.

Answer

The functools module provides higher-order functions and tools for working with callables. lru_cache(maxsize=128): memoization decorator — caches function results based on input, O(1) lookup. wraps(wrapped): preserves the original function's metadata (name, docstring) when wrapping it in a decorator. partial(func, *args, **kwargs): creates a new function with some arguments pre-filled: double = partial(multiply, 2). reduce(func, iterable, initial): reduces an iterable to a single value by applying a function cumulatively. total_ordering: given __eq__ and one comparison method, fills in the rest. cached_property: like @property but caches the result on first access. singledispatch: function overloading based on the type of the first argument.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Python codebase.