alon horev added the comment: I think the following test demonstrates the API we're looking for. 1. Am I missing any functionality? 2. How does partialmethod relate to @absolutemethod?
from functools import partial class partialmethod(object): def __init__(self, func, *args, **keywords): # func could be a descriptor like classmethod which isn't callable, # so we can't inherit from partial (it verifies func is callable) if isinstance(func, partialmethod): # flattening is mandatory in order to place cls/self before all other arguments # it's also more efficient since only one function will be called self.func = func.func self.args = func.args + args self.keywords = {} self.keywords.update(func.keywords) self.keywords.update(keywords) else: self.func = func self.args = args self.keywords = keywords def _make_unbound_method(self): def _method(*args, **keywords): keywords.update(self.keywords) cls_or_self, *rest = args call_args = (cls_or_self,) + self.args + tuple(rest) return self.func(*call_args, **keywords) return _method def __get__(self, obj, cls): get = getattr(self.func, "__get__", None) if get is None: if obj is None: return self._make_unbound_method() return partial(self._make_unbound_method(), obj) # returns a bound method else: callable = get(obj, cls) if callable is self.func: return self._make_unbound_method() return partial(callable, *self.args, **self.keywords) class A(object): def add(self, x, y): return self, x + y add10 = partialmethod(add, 10) add10class = partialmethod(classmethod(add), 10) add10static = partialmethod(staticmethod(add), 'self', 10) return15 = partialmethod(add10, 5) # nested partialmethod add2partial = partial(add, x=2) return12 = partialmethod(add2partial, y=10) # partialmethod over partial def test(): cls = A instance = cls() assert instance.add10class(5) == (cls, 15) assert cls.add10class(5) == (cls, 15) assert instance.add10static(5) == ('self', 15) assert cls.add10static(5) == ('self', 15) assert instance.add10(5) == (instance, 15) assert cls.add10(instance, 5) == (instance, 15) assert instance.return15() == (instance, 15) assert cls.return15(instance) == (instance, 15) assert instance.return12() == (instance, 12) assert cls.return12(instance) == (instance, 12) test() ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue4331> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com