Install & Compatibility
Where this runs
tested against v? · pip install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
build_error
glibcpy 3.10–3.920 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
lru_cache
✓ from functools import lru_cache
✗ from functools32 import lru_cache
After installing functools32, its features are intended to be imported directly from the standard `functools` module, as it effectively replaces or augments the built-in module on Python 2.7/PyPy.
partial
✓ from functools import partial
Like lru_cache, other backported features are imported directly from `functools`.
This example demonstrates how to use the `lru_cache` decorator provided by functools32 on Python 2.7. For Python 3 and newer, `lru_cache` is part of the standard `functools` module and `functools32` should not be used.
import sys
# This library is only for Python 2.7/PyPy.
# For modern Python, functools.lru_cache is built-in.
if sys.version_info.major < 3:
import functools
from functools import lru_cache
import time
@lru_cache(maxsize=128)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
print(f"Calculating fibonacci(30) on Python {sys.version_info.major}.{sys.version_info.minor}...")
start = time.time()
result = fibonacci(30)
end = time.time()
print(f"fibonacci(30) = {result}, took {end - start:.4f} seconds")
print(f"Cache info: {fibonacci.cache_info()}")
else:
print(f"functools32 is not needed on Python {sys.version_info.major}.{sys.version_info.minor}. Use built-in functools.")
Debug
Known issues
breakingfunctools32 is strictly for Python 2.7 and PyPy. Attempting to install or use it on Python 3 will lead to errors, as the functionality it provides is already built into Python 3's standard `functools` module.fixDo not install or use functools32 on Python 3. For Python 3, directly `import functools` to access features like `lru_cache`.
affects: Python 3.x
deprecatedThe entire library is considered deprecated due to Python 2.7 reaching its end-of-life and the complete integration of its features into modern Python 3. No further development or maintenance is expected.fixMigrate Python 2.7 applications to Python 3, where `functools` provides these features natively and is actively maintained.
affects: All versions
gotchaUsing mutable arguments (e.g., lists, dictionaries) with `lru_cache` will result in a `TypeError: unhashable type` because cached function arguments must be hashable to serve as dictionary keys.fixEnsure that all arguments passed to an `lru_cache` decorated function are immutable and hashable. If mutable data must be used, convert it to an immutable representation (e.g., tuple, frozenset) before passing it to the cached function.
affects: All versions (general `lru_cache` behavior)
gotchaWhile `lru_cache` is thread-safe for basic usage, a race condition can occur where the decorated function might be called more than once by different threads if an initial call has not yet completed and cached its result. `cached_property` in newer `functools` also has similar considerations.fixFor critical scenarios requiring strict single-call execution or for `cached_property` in multi-threaded contexts, implement explicit locking within the decorated function or around the cached property access.
affects: All versions (general `lru_cache` behavior)
Upgrade
Version history
3.2.3-2latest on PyPI · released Jul 11, 2015
Audit
Dependencies
Python 2.7 or PyPyrequiredThe library is a backport specifically for these older Python runtimes and is not intended for Python 3.