Is this PEP-able? (syntax for functools.partial-like functionality)

Fábio Santos fabiosantosart at gmail.com
Mon Aug 19 19:49:16 EDT 2013


I had an idea for a handy syntax which I thought of while designing a
language for fun. This would be a syntax for creating, from a function, a
function which is just like it but with some parameters pre-filled. The
usage is much like functools.partials, and my proposed syntax is like this:

def spam(a, b, c):
    ...

spam_with_defaults = spam{1, 2}

Now calling spam_with_defaults is just like calling spam, but it only needs
one argument since a and b were set to 1 and 2, respectively.

spam_with_defaults(3)  # same as calling spam(1, 2, 3)

This would also work with keyword arguments, E.G. spam{c=3} would return a
callable which would only need the arguments a and b.

This is just the plain old functools.partial functionality, but of course I
won't stop here. Still on our spam function:

spam_require_b = spam{2, *, 3}
spam_require_ab = spam{*, 3}

spam_require_a(1)  # same as spam(1, 2, 3)
spam_require_ab(1, 2)  # same as above

The * sign means that the function takes positional arguments which will be
added in place of the star. This is how we would do spam_require_b in pure
python:

def spam_require_b(*args, **kwargs):
    return spam(*([1] + args + [2]), **kwargs)

Or, since we know it's only one argument,

spam_require_b = lambda b: spam(1, b, 3)

I also propose unpacking:

spam_unpacking = spam{1, (*, *)}
c = map(spam_unpacking, some_dict.items())

(Although this syntax isn't final), and receiving specific keyword
arguments.

spam_kw = spam{a, b, c=*}

The use cases this is intended to serve are mostly iteration related. There
is the case for being good plumbing for functions such as map, sorted,
filter and itertools.takewhile.

lines = filter(str.startswith{*, '#'}, open('file.cfg'))
lines = filter(bool, map(str.strip, lines))
config = dict(map(str.split{*, '=', 1}, lines))

A secondary use case is the creation of aliases.

def baz(self, callback):
    respond = callback{instance=self}
    ...

What do you think?

PS: yes, I realized that I am proposing the addition of braces to the
language syntax.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130820/b750b243/attachment.html>


More information about the Python-list mailing list