[Python-ideas] A suggestion for Python 3 vs Python 2

Haoyi Li haoyi.sg at gmail.com
Thu Nov 14 05:17:33 CET 2013


> But to handle a vararg function, you'd need a separate syntax for
partializing vs. calling.

I personally like the _ notation used by scala; with
macros<https://github.com/lihaoyi/macropy#quick-lambdas>you could
easily write something like:

f[spam(_, 5)]
f[spam(_, n=_)]

Which desugars into

lambda x: spam(x, 5)
lambda x, y: spam(x, n=y)

If you were willing to special-case further, you could simply have

spam(_, 5)
spam(_, n=_)

be representative of the partial-application. Granted _ is already used for
other things (e.g. i18n) but that's a solvable problem (make i18n use __,
let partial application use $).




On Wed, Nov 13, 2013 at 7:13 PM, Andrew Barnert <abarnert at yahoo.com> wrote:

> On Nov 13, 2013, at 12:44, אלעזר <elazarg at gmail.com> wrote:
>
> > If it was part of a bigger feature, like
> > ML's curried functions syntax, it would have been great - things like:
> >
> >    perr = print sys.stderr
> >    perr "Bad command or file name"
>
> I know this is getting way off topic, but the real problem with doing
> curried/auto-partial functions in Python isn't the parens, it's the
> variable arguments. An auto-partial function has to accumulate parameters
> if it doesn't get enough, execute when it does. (Currying gives you that
> for free, because it means you only get one argument at a time, but you can
> do auto-partials without currying.)
>
> With print, how do you know when it has "enough" arguments?
>
> You can write a decorator that only works on functions with fixed
> parameter counts pretty easily:
>
>     @autopartial
>     def spam(word, n):
>         for _ in range(n):
>             print(word)
>
>     eggs = spam('eggs')
>     eggs(3)
>
> But to handle a vararg function, you'd need a separate syntax for
> partializing vs. calling.
>
> Using a different operator like [] or % or << seems attractive at first,
> but it can't handle keywords.
>
> You could add a method, so spam._(n=5) returns partial(spam, n=5), but ._
> is hideous, and anything meaningful like bind or partial is no longer a
> shortcut.
>
> You could use a special argument value, and ... looks perfect, especially
> as args[-1]: spam('eggs', ...). Until you consider keywords args, which
> come after args[-1]. So the best you can do is args[0]: spam(..., 'eggs',
> n=3). That isn't terrible, but I'm not sure it's nice enough to be worth
> the cost of people not understanding what it's doing.
>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131113/348ac470/attachment.html>


More information about the Python-ideas mailing list