Question presented to candidate:
"Tell me about functools. Which parts of it do you actually use, and what problem does each one solve?"
What a strong answer should cover:
functoolsholds higher-order functions: tools that take functions and return functions (or callable objects), for adapting, caching and dispatching.- Adapting:
partial/partialmethodfreeze arguments;wraps(viaupdate_wrapper) copies__name__,__doc__and sets__wrapped__on decorator wrappers;reducefolds a sequence;cmp_to_keyadapts old compare functions forsorted. - Caching:
lru_cache(maxsize=...)memoises with LRU eviction andcache_info();cache(3.9) is the unbounded version;cached_property(3.8) computes once per instance and stores the result in the instance__dict__. - Classes and dispatch:
total_orderingfills in missing comparison methods from__eq__plus one of them;singledispatch/singledispatchmethodchoose an implementation by the type of the first argument. - Trade-offs: cached arguments must be hashable, caches keep references alive (memory leaks on methods),
cached_propertyneeds a__dict__,partialis preferable to alambdabecause it is introspectable and picklable.
Code / implementation expected: Optional β a partial, a wraps-based decorator, or an lru_cache with cache_info().