[Python-ideas] Syntactic sugar to declare partial functions
Steven D'Aprano
steve at pearwood.info
Tue Aug 14 06:22:13 EDT 2018
On Mon, Aug 13, 2018 at 07:46:49PM +0200, Stefan Behnel wrote:
> Michel Desmoulin schrieb am 09.08.2018 um 18:59:
> > I'd rather have functools.partial() to be added as a new method on
> > function objects.
[...]
> > add_2 = add.partial(2)
>
> Except that this only works for functions, not for other callables.
> Meaning, code that uses this for anything but its self-defined functions
> will break as soon as someone passes in a callable object that is not a
> function.
That's an excellent point.
If it were important to cover those use-cases, we could add a partial
method to types, methods, builtin-functions etc. But I suspect that
doing that would increase the cost of this proposed feature past the
point where it is worthwhile.
Do we often call functools.partial on arbitrary callable objects that we
don't know in advance? (Not a rhetorical question.) I know I don't --
all the times I've called partial, I've known what the callable was, and
it was always a function I created with def. But maybe others do
something differently.
I don't mind writing this:
# only a minor inconvenience
spam = func.partial(arg)
eggs = functools.partial(callable, arg)
given that I knew upfront that func was a function and callable was not.
But if I didn't know, the utility of a partial method would be hugely
reduced, and I'd just use functools.partial for everything.
--
Steve
More information about the Python-ideas
mailing list