PEP 309 (Partial Function Application) Idea

Steven Bethard steven.bethard at gmail.com
Sat Mar 12 15:33:49 EST 2005


Chris Perkins wrote:
[snip implementation]
> While I think that func(x, ...) is more readable than partial(func, x),
> I'm not sure that I would use either of them often enough to warrant
> special syntax.

Interesting.  Thought it might be worth listing a few of the current 
places people use lambdas (and lambda replacements like 
operator.attrgetter/itemgetter) that this might be useful, and a few of 
the places where it probably wouldn't be.

Places where it might be useful:

getting attributes:
     lambda obj: obj.attr
     attrgetter('attr')
     getattr(__, attr)

getting attributes with defaults[1]:
     objs.sort(key=lambda a: getattr(a, 'lineno', 0))
     objs.sort(key=getattr(__, 'lineno', 0)

Places where you might be able to use it (with some changes):

using bound methods[1][2]:
     map(lambda x: x.strip(), lst)
     map(str.strip(), lst) #!! doesn't work for unicode
     map(methodcaller('strip'), lst) # proposed by Alex Martelli
     __.strip() # note that calling strip on __ would have to
                # return a curryable looking for one arg...

Places where I can't see how to use it:

creating a function out of nothing[2]:
     button.setlabel(lambda: 'Click Me!')
     button.setlabel('Click Me!'.__str__) # works 'cause str returns self

adding arguments[1]:
     lambda x: ""

adding method to an instance[1]:
     self.plural = lambda n: int(n != 1)

So I guess it's a cool idea, but I don't know if it's really going to 
pacify anyone who is upset about losing lambda...  (Not that I'm 
suggesting that was your intention -- but it's something that's been 
recently on my mind.)

Steve

[1]http://mail.python.org/pipermail/python-list/2004-December/257990.html
[2]http://www.artima.com/forums/flat.jsp?forum=106&thread=98196



More information about the Python-list mailing list