| id |
python/prebound_method |
| name |
Prebound Method |
| aliases |
|
| guide_url |
https://python-patterns.guide/python/prebound-methods/ |
| problem |
Offer module-level functions that share state, by binding the methods of one hidden instance to module globals. |
| symptoms |
module-level API over shared state |
random.random-style interface |
convenience functions plus an instantiable class |
|
| verdict |
pythonic |
| caveats |
Build the hidden instance cheaply and without I/O — it is constructed at import time. |
Keep the class public too, so users needing isolated state can instantiate their own (exactly as random.Random allows). |
|
| stdlib_sightings |
random.random |
random.seed |
secrets.choice |
|
A random.random()-style module API: one hidden instance built at import,
its bound methods published as module functions, the class kept public for
isolation. Verdict: pythonic — the stdlib's own favorite move.
uv run python -m patterns.python.prebound_method.examples.metrics.main