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: 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: =?ISO-8859-1?Q?F=E1bio_Santos?= 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --001a11c252c69c061004e4559623 Content-Type: text/plain; charset=ISO-8859-1 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. --001a11c252c69c061004e4559623 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

I had an idea for a handy syntax which I thought of while de= signing a language for fun. This would be a syntax for creating, from a fun= ction, 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):
=A0=A0=A0 ...

spam_with_defaults =3D spam{1, 2}

Now calling spam_with_defaults is just like calling spam, bu= t it only needs one argument since a and b were set to 1 and 2, respectivel= y.

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

This would also work with keyword arguments, E.G. spam{c=3D3= } 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 =3D spam{2, *, 3}
spam_require_ab =3D spam{*, 3}

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

The * sign means that the function takes positional argument= s which will be added in place of the star. This is how we would do spam_re= quire_b in pure python:

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

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

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

I also propose unpacking:

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

(Although this syntax isn't final), and receiving specif= ic keyword arguments.

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

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

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

A secondary use case is the creation of aliases.

def baz(self, callback):
=A0=A0=A0 respond =3D callback{instance=3Dself}
=A0=A0=A0 ...

What do you think?

PS: yes, I realized that I am proposing the addition of brac= es to the language syntax.

--001a11c252c69c061004e4559623--