Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52709

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <fabiosantosart@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'parameters': 0.04; 'syntax': 0.04; 'argument': 0.05; 'args': 0.07; 'subject:PEP': 0.07; 'arguments': 0.09; 'arguments,': 0.09; 'function,': 0.09; 'function:': 0.09; 'python:': 0.09; 'subject:skip:f 10': 0.09; 'def': 0.12; 'mostly': 0.14; '**kwargs)': 0.16; '**kwargs):': 0.16; 'argument,': 0.16; 'braces': 0.16; 'c):': 0.16; 'callable': 0.16; 'creating,': 0.16; 'iteration': 0.16; 'lambda': 0.16; 'positional': 0.16; 'star.': 0.16; 'think?': 0.16; 'language': 0.16; '8bit%:5': 0.22; 'proposed': 0.22; 'config': 0.24; 'propose': 0.24; 'subject:like': 0.24; 'this:': 0.26; 'idea': 0.28; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; 'serve': 0.31; '(although': 0.31; 'proposing': 0.31; 'spam,': 0.31; 'cases': 0.33; 'plain': 0.33; 'skip:d 20': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'keyword': 0.36; 'being': 0.38; 'e.g.': 0.38; 'filter': 0.38; 'to:addr:python-list': 0.38; '\xa0\xa0\xa0': 0.39; 'to:addr:python.org': 0.39; '8bit%:6': 0.40; 'how': 0.40; 'course': 0.61; 'such': 0.63; 'our': 0.64; 'realized': 0.68; 'subject:this': 0.83
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=AJ/PNSg+7vyScWwQDXEVNOVHgOcSpUtz9NoLQ7Xjj0Y=; b=Rs3ERRKa+HgdFmkqYDxbVQ5ypoJPLv4WJJA7nNvOcBU7GDwY9tYkrmbNdTLkfZSxJY e+uWC1o/4nov96H86Yg1yfx3edaC/P56arfzTpX2ILdISubIFotebgh5LwrspRDBUzoK pmR89Tn++jdLPtZjQtO0XBTPPO1O9pAinyoiSCcdysZu5ZJDRFMqvV6f2e9o2RqG0RSn K9uv0XV0YffgfVCgYvUIPDXflydM0vtcYUPUMC8EisAHlZWttbOYBDaMScRPi/FvDrul AwqR2wDCoGYmGCFrLTvOifDzsdehkAKJxRl/HUMVlf3FA5yt7CabTTTayGrUCOuD6fvy dtYA==
MIME-Version 1.0
X-Received by 10.224.24.134 with SMTP id v6mr453778qab.58.1376956156999; Mon, 19 Aug 2013 16:49:16 -0700 (PDT)
Date Tue, 20 Aug 2013 00:49:16 +0100
Subject Is this PEP-able? (syntax for functools.partial-like functionality)
From Fábio Santos <fabiosantosart@gmail.com>
To python-list@python.org
Content-Type multipart/alternative; boundary=001a11c252c69c061004e4559623
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.43.1376956165.19984.python-list@python.org> (permalink)
Lines 124
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1376956165 news.xs4all.nl 15950 [2001:888:2000:d::a6]:43446
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52709

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Is this PEP-able? (syntax for functools.partial-like functionality) Fábio Santos <fabiosantosart@gmail.com> - 2013-08-20 00:49 +0100
  Re: Is this PEP-able? (syntax for functools.partial-like functionality) Steven D'Aprano <steve@pearwood.info> - 2013-08-20 06:16 +0000
    Re: Is this PEP-able? (syntax for functools.partial-like functionality) Fábio Santos <fabiosantosart@gmail.com> - 2013-08-20 09:28 +0100
    Re: Is this PEP-able? (syntax for functools.partial-like functionality) Ian Kelly <ian.g.kelly@gmail.com> - 2013-08-20 03:11 -0600
    Re: Is this PEP-able? (syntax for functools.partial-like functionality) Fábio Santos <fabiosantosart@gmail.com> - 2013-08-20 10:25 +0100

csiph-web