[Python-Dev] PEP 309: Partial method application
Ian Bicking
ianb at colorstudy.com
Thu Aug 18 17:54:49 CEST 2005
I missed the discussion on this
(http://www.python.org/peps/pep-0309.html), but then 2.5 isn't out yet.
I think partial() misses an important use case of method getting, for
instance:
lst = ['A', 'b', 'C']
lst.sort(key=partialmethod('lower'))
Which sorts by lower-case. Of course you can use str.lower, except
you'll have unnecessarily enforced a type (and excluded Unicode). So
you are left with lambda x: x.lower().
Here's an implementation:
def partialmethod(method, *args, **kw):
def call(obj, *more_args, **more_kw):
call_kw = kw.copy()
call_kw.update(more_kw)
return getattr(obj, method)(*(arg+more_args), **call_kw)
return call
This is obviously related to partial(). Maybe this implementation
should be a classmethod or function attribute, partial.method().
--
Ian Bicking / ianb at colorstudy.com / http://blog.ianbicking.org
More information about the Python-Dev
mailing list