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.

Usage

from anymethod import anymethod

class FooBar:
    @anymethod
    def who_is_my_owner(owner) -> None:
        print(owner)

Installation

$ pip install anymethod

Example

>>> from anymethod import *

>>> class FooBar:
...     @anymethod
...     def whoami(owner) -> None:
...         me = 'cls' if isinstance(owner, type) else 'obj'
...         print(me, owner)

>>> FooBar.whoami()
cls <class '__main__.FooBar'>

>>> FooBar().whoami()
obj <__main__.FooBar object ...>

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for anymethod-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0f865ba3296c9e8f6379f2fe8e531fe994c410d99ab504591e5550296363ca28
MD5 feed80f083f2fd9265d8c4dab2b45177
BLAKE2b-256 3ed573c44838c45dd64b08c13ad536c0ba317299cd9c3d7e19c02784a7798b5a

See more details on using hashes here.

File details

Details for the file anymethod-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: anymethod-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.13

File hashes

Hashes for anymethod-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 777d9c3b1a5f4ed4b7ce44077e0a11ab4e37b282d97e10f8286f5487587ce838
MD5 e55a1b6c19cf0d9e512e0fe2d2899fd0
BLAKE2b-256 672c0f84db70258846db49e8cc56a4dd36a55e282daa267cbad4f5906fd0e57a

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