[Python-ideas] A suggestion for Python 3 vs Python 2
Andrew Barnert
abarnert at yahoo.com
Thu Nov 14 04:13:19 CET 2013
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.
More information about the Python-ideas
mailing list