PEP 309 (Partial Function Application) Idea

Chris Perkins chrisperkins99 at gmail.com
Mon Mar 14 10:32:06 EST 2005


Steven Bethard <steven.bethard at gmail.com> wrote:
> 
> getting attributes with defaults[1]:
>      objs.sort(key=lambda a: getattr(a, 'lineno', 0))
>      objs.sort(key=getattr(__, 'lineno', 0)
> 

Yes, this exact example is one of the (very) few that I found in the
standard library where the syntax actually helps.

> 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...
> 

Yes, the bound method problem is what convinced me to give up on this.
One can hardly write "....strip()" (yes, that's four dots).
But note that there is a library out there that does precisely this
(and much more, if I remember) - I believe it's in David Mertz's
Xoltar toolkit. It does allow you to write "_.strip()" and get a
curried callable (He uses "_", a single underscore, as the magic
thingy, which is odd considering its established meaning in
interactive mode).

If we could add an object to the builtins (or elsewhere) called "__"
that works as I proposed for "...", and also behaves like the things
in David Mertz's library (sorry, don't remember exactly where to find
it, or what it's called, and don't have time to look it up right now),
then this might really get interesting. Then we could do any of:
getattr(__, 'lineno', 0)
__.strip()
__.foo().bar(__)

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

and _maybe_ even:
self.plural = int(__ != 1)


But wait - on second thought, "__" is a legal identifier now, unlike
"...", so we can't have the compiler messing around with expressions
like foo(x, __), can we.  OK, now I really give up on the whole idea!


Chris Perkins



More information about the Python-list mailing list