ExtendAnything
ExtendAnything is a small Python helper for wrapping an already-created object with a new interface. A wrapper class can add methods, override selected method names, and still expose the original object's attributes without copying or reinitializing the original instance.
The package is currently pre-alpha (0.0.1) and requires Python 3.8 or newer.
Why it exists
Normal subclassing only helps before an object is created. ExtendAnything is for cases where the instance already exists, such as a trained model, configured client, or object returned by another library, and you want to attach project-specific behavior around it.
Conceptually, it behaves like a lightweight dynamic cast: the wrapper handles the behavior it defines, and missing attribute lookups fall through to the wrapped object.
How it works
Subclass ExtendAnything, accept the object to wrap, and call
super().__init__(inner). The base class stores the original object on
self._inner and implements __getattr__ so attributes and methods not found on
the wrapper are read from self._inner.
from extendanything import ExtendAnything
class Predictor:
def __init__(self, name):
self.name = name
def predict(self):
return "base prediction"
class EnhancedPredictor(ExtendAnything):
def __init__(self, inner, prefix):
super().__init__(inner)
self.prefix = prefix
def predict(self):
return f"{self.prefix}: {self._inner.predict()}"
def explain(self):
return f"{self.name} -> {self.predict()}"
base = Predictor("demo")
wrapped = EnhancedPredictor(base, "custom")
assert wrapped.name == "demo" # forwarded to base
assert wrapped.predict() == "custom: base prediction" # wrapper override
assert wrapped.explain() == "demo -> custom: base prediction" # wrapper-only method
The wrapper also defines __getstate__ and __setstate__, so wrapped objects can
round-trip through pickle and joblib when the wrapped object itself supports
that.
Installation
The runtime package has no required third-party dependencies.
pip install extendanything
For local development from this repository:
pip install -r requirements_dev.txt
pip install -e .
Important behavior
- Reads of missing attributes are forwarded to
self._inner. - Methods and attributes defined on the wrapper take precedence over the wrapped object.
- Assigning
wrapped.attr = valuewrites to the wrapper, not to the inner object. To mutate the wrapped object, assign throughwrapped._inner.attr. - If the inner object changes, forwarded reads reflect those changes unless the wrapper has already set an attribute with the same name.
- Methods running inside the inner object still use the inner object's own
method resolution. If an inner method calls
self.predict(), it will not call apredict()override defined on the wrapper. - The wrapper is not registered as a subclass of the wrapped object's class, so
isinstance(wrapped, InnerClass)is false. - Special methods such as indexing are not generally forwarded; use
wrapped._inner[...]when direct access to the original object is needed.
Development
make test
make lint
make docs
Tests cover attribute forwarding, wrapper overrides, attribute detachment,
repr, and pickle/joblib serialization.
ExtendAnything is released under the MIT license.
Changelog
0.0.1
- First release on PyPI.
Metadata
Release files for extendanything 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| extendanything-0.0.2.tar.gz | 12.0 kB | Without provenance | Details for extendanything-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| extendanything-0.0.2-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for extendanything-0.0.2-py2.py3-none-any.whl |
Total release size: 17.8 kB
Release files / extendanything-0.0.2.tar.gz
| Download URL | extendanything-0.0.2.tar.gz |
|---|---|
| Size | 12.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e03e91ad63e54bcc1f765b95dfcb3ae5516b3801153284a49a51268fe63e6f77
|
|
BLAKE2b-256 checksum How to use checksums |
8b6fc532b41dc989ab8107a562873463ad84bcd4c1aa966948a5ea5450af262a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / extendanything-0.0.2-py2.py3-none-any.whl
| Download URL | extendanything-0.0.2-py2.py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
7d178f3c03e2e3237e2adab11a3a46e8876f600530b714232d472b0d103e7a35
|
|
BLAKE2b-256 checksum How to use checksums |
85df09cd78742012fdec6523314ecc46d54581c1cc0949f53314ef30be341936
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Security for extendanything 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.