A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/2005-August/055495.html below:

[Python-Dev] PEP 309: Partial method application

[Python-Dev] PEP 309: Partial method applicationIan 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4