Skip to main content

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()

Release files for classmethod-decorator 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for classmethod-decorator 0.2.0
File Size Uploaded
classmethod_decorator-0.2.0.tar.gz 2.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for classmethod-decorator 0.2.0
File Interpreter ABI Platform
classmethod_decorator-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 5.1 kB

Release files / classmethod_decorator-0.2.0.tar.gz

Download URL classmethod_decorator-0.2.0.tar.gz
Size 2.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1dbb6ee73e9f2f8e43f9df46882a2648f9711eb26b587a74e662457cd52b8fc7
BLAKE2b-256 checksum
How to use checksums
172d1dc19ee59be3492ee22fd5ac77d2375ebed94d555c938455e65ba46b19f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.5 Linux/5.15.153.1-microsoft-standard-WSL2

Release files / classmethod_decorator-0.2.0-py3-none-any.whl

Download URL classmethod_decorator-0.2.0-py3-none-any.whl
Size 2.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5575568c9b93a5ba3827e2ae9e32911f403ae56912818b4759165e4136fde35a
BLAKE2b-256 checksum
How to use checksums
812c01a4162a0df961e2b33db75d6a86e8d31a55786f7c7d0e1c45c067da4ede
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.5 Linux/5.15.153.1-microsoft-standard-WSL2

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page