anymethod
@anymethod = instance method + @classmethod
For the cases when class and its instances can be used interchangeably, code duplication can be avoided if the same member function works both as @classmethod and instance method.
This can be easily achieved with Python descriptors.
Installation
$ pip install anymethod
Example A
>>> from anymethod import anymethod
>>> class FooBar:
... @anymethod
... def whoami[O](owner: O) -> O:
... return owner
>>> FooBar.whoami()
<class '__main__.FooBar'>
>>> FooBar().whoami()
<__main__.FooBar object at 0x...>
Example B
from typing import Any, ClassVar
from anymethod import anymethod
class FooBar:
_cls: ClassVar[list[Any]] = []
_obj: list[Any]
def __init__(self) -> None:
self._obj = []
@anymethod
def add_value(owner, v: Any) -> None:
if isinstance(owner, type):
owner._cls.append(v)
else:
owner._obj.append(v)
More info
Check the Project changelog
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
anymethod-0.1.2.tar.gz
(58.8 kB
view details)
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 anymethod-0.1.2.tar.gz.
File metadata
- Download URL: anymethod-0.1.2.tar.gz
- Upload date:
- Size: 58.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.5.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32b663dc654e5934cf2a9d4bde6cbf85196a9678b776b630a344798d5ecb4e00
|
|
| MD5 |
6c76e1b4ec0771877ec3ebe8da67c054
|
|
| BLAKE2b-256 |
46a25bb9378e6899badbc6af275c012b2723d283ae5315fe7b360c5b159c81ce
|
File details
Details for the file anymethod-0.1.2-py2.py3-none-any.whl.
File metadata
- Download URL: anymethod-0.1.2-py2.py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.5.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebdf2904ea569be1317f887f2342dc888baba1f45e2f6e9cc5bdc296880ab91d
|
|
| MD5 |
0a3bc7799d389fce850ffbaaf87d2049
|
|
| BLAKE2b-256 |
3edccf3a4d8f0955bbeb72c109e0bf3cb16147a7753918f4aa57186c6a3d19ab
|