Mimics C#-style extension methods in Python.
Project description
extensionmethods
Mimics C#-style extension methods in Python.
extensionmethods is a tiny package that lets you “attach” functions to existing types without modifying their source code, enabling method-like syntax, better chaining, and cleaner separation of optional dependencies.
Example usage
In C#, you can add methods to existing types:
public static class IntExtensions
{
public static int Double(this int x)
{
return x * 2;
}
}
int result = 7.Double();
You get method syntax without modifying int.
With the extensionmethods package you can achieve similiar functionality like so:
from extensionmethods import extension
@extension
def double(x: int) -> int:
return x * 2
result = 7 | double()
print(result) # 14
With parameters:
@extension
def add_then_multiply(x: int, to_add: int, to_multiply: int) -> int:
return (x + to_add) * to_multiply
result = 7 | add_then_multiply(11, 3)
print(result) # 54
The value on the left side becomes the first argument of the function.
Type safety and IDE integration
The extension methods are type-aware, based on the type of their first parameter. Type checkers and editors can detect incorrect usage:
"hello" | double() # type error
Your IDE (e.g. VS Code) can flag this because the extension has its first argument of type int, not str.
Furtermore, the extension methods support docstrings and code suggestions in your IDE:
Installation
Using pip:
pip install extensionmethods
Using uv:
uv pip install extensionmethods
Why use extension methods?
Readability through chaining
Instead of nested calls:
result = normalize(scale(center(data)))
You can express the same flow step-by-step:
result = data | center() | scale() | normalize()
This reads left-to-right and mirrors how data is conceptually transformed.
Modularity and dependency isolation
Suppose you maintain a core class:
class Dataset:
...
You want export helpers:
to_pandas()to_numpy()to_torch()
If you put these methods directly on Dataset, your core package must depend on pandas, numpy, and torch.
Instead, keep the core dependency-free:
# core package
class Dataset:
...
Then provide optional extensions:
# dataset_pandas package
import pandas as pd
from extensionmethods import extension
from core import Dataset
@extension(to=Dataset)
def to_pandas(ds: Dataset) -> pd.DataFrame:
...
Usage:
import dataset_pandas # registers the extension
df = dataset | to_pandas()
Now:
- The core package has zero heavy dependencies
- Users only install what they need
- Functionality stays logically grouped
Known caveats
Uses the | operator (__ror__)
The system works by overriding the right-side bitwise OR operator.
result = value | extension_call()
This only works if the left-hand type does not fully consume the | operator itself.
For example, sets already use |:
{1, 2} | {3} # set union
If a type defines its own __or__ in a way that prevents fallback to __ror__, the extension method will not run.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file extensionmethods-0.1.5.tar.gz.
File metadata
- Download URL: extensionmethods-0.1.5.tar.gz
- Upload date:
- Size: 51.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5dd1601e6f5159bd41de9a24881e567d36bfb4c69026eddd73d0c78d51a2ebcb
|
|
| MD5 |
3429860269c607790d67ef986743290b
|
|
| BLAKE2b-256 |
093a12a6e3560ea00981ada787f6d9012fb0f1f9889e874f3f7ce27621fc5654
|
Provenance
The following attestation bundles were made for extensionmethods-0.1.5.tar.gz:
Publisher:
build-test-publish.yml on Pim-Mostert/extensionmethods
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
extensionmethods-0.1.5.tar.gz -
Subject digest:
5dd1601e6f5159bd41de9a24881e567d36bfb4c69026eddd73d0c78d51a2ebcb - Sigstore transparency entry: 953615680
- Sigstore integration time:
-
Permalink:
Pim-Mostert/extensionmethods@4dde3aaf870254e308aa1c8ecb790571a5883857 -
Branch / Tag:
refs/tags/v0.1.5 - Owner: https://github.com/Pim-Mostert
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-test-publish.yml@4dde3aaf870254e308aa1c8ecb790571a5883857 -
Trigger Event:
push
-
Statement type:
File details
Details for the file extensionmethods-0.1.5-py3-none-any.whl.
File metadata
- Download URL: extensionmethods-0.1.5-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71c593906ee90e7ba4db39d08f9127deb6f9f12cebe93753641fb3069214db20
|
|
| MD5 |
5bf16138b546903cd803fa8edf46ae14
|
|
| BLAKE2b-256 |
e37a5cdcc2b799d7e6f769b09aae404bf15cf3ec3e1d6d611d36ae3e906d9b09
|
Provenance
The following attestation bundles were made for extensionmethods-0.1.5-py3-none-any.whl:
Publisher:
build-test-publish.yml on Pim-Mostert/extensionmethods
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
extensionmethods-0.1.5-py3-none-any.whl -
Subject digest:
71c593906ee90e7ba4db39d08f9127deb6f9f12cebe93753641fb3069214db20 - Sigstore transparency entry: 953615683
- Sigstore integration time:
-
Permalink:
Pim-Mostert/extensionmethods@4dde3aaf870254e308aa1c8ecb790571a5883857 -
Branch / Tag:
refs/tags/v0.1.5 - Owner: https://github.com/Pim-Mostert
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-test-publish.yml@4dde3aaf870254e308aa1c8ecb790571a5883857 -
Trigger Event:
push
-
Statement type: