Registry / serialization / methodtools

methodtools

JSON →
library0.4.7pypypi✓ verified 25d ago

Methodtools extends standard `functools` functionality to class methods, providing decorators like `cached_method`, `lru_method`, `weak_method`, and `locked_method`. It simplifies applying common patterns (caching, locking) directly to instance methods. It is currently at version 0.4.7 and has an active, consistent release cadence.

pip install methodtools
INSTALL
IMPORT
SIG · METHODTOOLS
M
methodtools
serializationpythonv0.4.7
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

cached_method
from methodtools import cached_method
from methodtools import cached_method
lru_cache
from methodtools import lru_cache
from methodtools import lru_cache
Wire
from methodtools import Wire
from methodtools import Wire

This example demonstrates how to use `cached_method` to memoize the result of an instance method. Subsequent calls with the same arguments will return the cached result without re-executing the method logic. The cache is managed per instance.

from methodtools import cached_method class MyClass: def __init__(self, value): self._value = value self._compute_count = 0 @cached_method def compute_something(self): """A method that computes something expensive.""" self._compute_count += 1 return self._value * 2 obj = MyClass(10) print(f"First call: {obj.compute_something()}") print(f"Second call (cached): {obj.compute_something()}") print(f"Compute count (should be 1): {obj._compute_count}") obj2 = MyClass(20) print(f"First call for obj2: {obj2.compute_something()}") print(f"Compute count for obj2 (should be 1): {obj2._compute_count}")
Debug
Known issues
gotchaWhen combining `methodtools` decorators with `@property`, `@staticmethod`, or `@classmethod`, the order often matters. Generally, `methodtools` decorators should be applied *before* `@property` (i.e., `methodtools` decorator innermost). Incorrect order can lead to caching the property descriptor itself rather than its computed value.
fix
Ensure `methodtools` decorators are applied closer to the method definition: `@property \n @cached_method \n def my_method(...)`
affects: All versions
gotchaLike `functools.cache`, `cached_method` relies on argument hashability. If a method takes mutable arguments (e.g., lists, dictionaries) and their *contents* change after the first call, the cached result will be returned, not reflecting the changed input. This can lead to stale data.
fix
Avoid passing mutable arguments directly to cached methods, or ensure their contents are not modified after the first call. Alternatively, use tuples for mutable sequences or implement custom hashing logic if necessary.
affects: All versions
gotchaFor `cached_method` and `lru_method`, the cache is managed per instance. To manually clear the cache for a specific instance's method, you need to access the underlying wrapped function. Direct attribute deletion won't work.
fix
To clear a method's cache for a specific instance, use `instance.method_name.__wrapped__.cache_clear()`.
affects: All versions
breakingThe component `cached_method` could not be imported from the `methodtools` library. This indicates a potential issue with the installed version of `methodtools`, its compatibility with the Python environment, or an unexpected change in the library's public API, preventing the library's core features from being used.
fix
Verify the installed `methodtools` version, check its changelog for API changes, or ensure correct installation and compatibility with the Python version. Consider trying a different version of `methodtools` or ensuring all dependencies are met.
affects: Unknown (specific to environment configuration or library version)
breakingThe `cached_method` decorator was introduced in `methodtools` version 0.3.0. An `ImportError` for `cached_method` indicates that an older version of the library is installed, which does not provide this feature.
fix
Upgrade `methodtools` to version 0.3.0 or later (e.g., `pip install --upgrade methodtools`).
affects: Prior to 0.3.0
Upgrade
Version history
0.4.7latest on PyPI · released Aug 23, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
20
Resources