Skip to main content

@anymethod = instance method and @classmethod

Project description

anymethod

@anymethod = instance method + @classmethod

license pypi python versions tests coverage tested with multipython uses docsub

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

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

anymethod-0.1.2.tar.gz (58.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

anymethod-0.1.2-py2.py3-none-any.whl (4.0 kB view details)

Uploaded Python 2Python 3

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

Hashes for anymethod-0.1.2.tar.gz
Algorithm Hash digest
SHA256 32b663dc654e5934cf2a9d4bde6cbf85196a9678b776b630a344798d5ecb4e00
MD5 6c76e1b4ec0771877ec3ebe8da67c054
BLAKE2b-256 46a25bb9378e6899badbc6af275c012b2723d283ae5315fe7b360c5b159c81ce

See more details on using hashes here.

File details

Details for the file anymethod-0.1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for anymethod-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ebdf2904ea569be1317f887f2342dc888baba1f45e2f6e9cc5bdc296880ab91d
MD5 0a3bc7799d389fce850ffbaaf87d2049
BLAKE2b-256 3edccf3a4d8f0955bbeb72c109e0bf3cb16147a7753918f4aa57186c6a3d19ab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page