functools32

3.2.3-2 · deprecated · verified Wed Apr 15

functools32 is a backport of the `functools` standard library module from Python 3.2.3, designed for use with Python 2.7 and PyPy. Its primary purpose is to provide features like `lru_cache` (Least-recently-used cache decorator) to older Python environments that lack them natively. The library saw its last release in July 2015 and is no longer actively maintained, as its features are standard in Python 3+ versions.

Warnings

Install

Imports

Quickstart

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.")

view raw JSON →