Skip to main content

No project description provided

Project description

Classmethod Decorators

A small Python hack that allows using a @classmethod within the same class as a decorator for other methods in the same class, which is not typically possible because functions are turned into methods once the class definition is complete.

Installation

pip install classmethod_decorators

Usage

from typing import Callable
from typing_extensions import Self
from classmethod_decorators import enable_classmethod_decorators, classmethod_decorator

@enable_classmethod_decorators
class Foo:
    @classmethod
    def register(cls, method: Callable[[Self], None]) -> Callable[[Self], None]:
        print(f"{method} is registered")

        def decorated(self: Self) -> None:
            print(f"decorated {method}")
            method(self)

        return decorated

    @classmethod_decorator(register)
    def method(self) -> None:
        print("method")

Foo().method()

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

classmethod_decorator-0.1.0.tar.gz (1.6 kB view details)

Uploaded Source

Built Distribution

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

classmethod_decorator-0.1.0-py3-none-any.whl (2.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: classmethod_decorator-0.1.0.tar.gz
  • Upload date:
  • Size: 1.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.5

File hashes

Hashes for classmethod_decorator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 834ec2c3149727d3db0e1880f950af54be5c02ba3bb0000c1a11171b2114447b
MD5 c09e5cc7d5b0006341f15779d740b5d3
BLAKE2b-256 82a4ad8096984130ada2f634d8bd1e562eb6bd6af36afad2d5f134fcc80701e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for classmethod_decorator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 66ed0c3ae5d37013991d641293d3901f1f677980da2ea7e818927c5fb868816c
MD5 b5a1fa479c63180e17c1c5d9593fa086
BLAKE2b-256 11d0db3cef21b693e493bd6a1b3d501203ff09df457897083358d44c46494629

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