Message201111
To clarify the current state of this:
- I'm still in favour of adding this feature for Python 3.4
- a suitable patch is still needed, as the currently attached patches modify the existing functools.partial object, rather than adding a separate "partialmethod" API
- a Python implementation would be fine
The following prototype should work as a starting point to be elaborated into a full patch with docs and tests:
class partialmethod(functools.partial):
def __get__(self, obj, cls):
if obj is None:
return self
return functools.partial(self.func,
*((obj,) + self.args),
**self.keywords)
def __call__(*args, **kwds):
self, *args = args
call_kwds = {}
call_kwds.update(self.keywords)
call_kwds.update(kwds)
return self.func(self,
*(self.args + args),
**call_kwds)
class C:
def example(self, *args, **kwds):
print(self, args, kwds)
fails = functools.partial(example, 1, 2, 3, x=1)
works = partialmethod(example, 1, 2, 3, x=1)
>>> C().fails()
1 (2, 3) {'x': 1}
>>> C().works()
<__main__.C object at 0x7f91cefeea90> (1, 2, 3) {'x': 1}
>>> C().fails(4, 5, 6)
1 (2, 3, 4, 5, 6) {'x': 1}
>>> C().works(4, 5, 6)
<__main__.C object at 0x7f91cefeea10> (1, 2, 3, 4, 5, 6) {'x': 1} |
|
Date |
User |
Action |
Args |
2013-10-24 10:33:01 | ncoghlan | set | recipients:
+ ncoghlan, rhettinger, jcea, belopolsky, ironfroggy, jackdied, Christophe Simonis, ssadler, eckhardt, r.david.murray, Alexander.Belopolsky, anacrolix |
2013-10-24 10:33:01 | ncoghlan | set | messageid: <1382610781.43.0.10906696951.issue4331@psf.upfronthosting.co.za> |
2013-10-24 10:33:01 | ncoghlan | link | issue4331 messages |
2013-10-24 10:33:01 | ncoghlan | create | |
|