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.1.tar.gz (58.5 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.1-py2.py3-none-any.whl (4.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file anymethod-0.1.1.tar.gz.

File metadata

  • Download URL: anymethod-0.1.1.tar.gz
  • Upload date:
  • Size: 58.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.13

File hashes

Hashes for anymethod-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0124cfd7e6429ef06b7e5e6e877104a9b5f0babdf3f6c50e773c214e12b7572d
MD5 92d53170a828b045099784c89d4747a3
BLAKE2b-256 85a0413a76e82b3c3ed50972212d41466015c286c8646d21b70e92ee830d16cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for anymethod-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 84ea3890d612b293afa107a6ed7cb515e165da7b6ae6b70ddc4d4c5d72e7b572
MD5 ce8a129f32f6d91b6de0e637e7162757
BLAKE2b-256 3a5773f97c48fe55210181401ff1fc457eab005fc45a3bd3d3a7b842f2cd3d22

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