py-fp/lib/classmethod.py
2026-04-10 00:21:44 +02:00

30 lines
No EOL
864 B
Python

from __future__ import annotations
class Classmethod:
def __init__(self, func):
self.func = func
self._attribute_name: str = getattr(func, "__name__", "<classmethod>")
def __set_name__(self, owner, name: str):
self._attribute_name = name
def __get__(self, instance, owner=None):
if owner is None:
owner = type(instance)
func = self.func
attr = self._attribute_name
def bound(*args, **kwargs):
return func(owner, *args, **kwargs)
bound.__name__ = attr
bound.__qualname__ = f"{owner.__qualname__}.{attr}"
return bound
def __set__(self, instance, value):
raise AttributeError("Classmethod reassigend")
def __repr__(self) -> str:
return f"Classmethod({self.func!r})"
Classmthod = Classmethod