[Python-ideas] Addition to operator module: starcaller
Nick Coghlan
ncoghlan at gmail.com
Thu Jul 21 11:20:38 EDT 2016
On 22 July 2016 at 00:19, Emanuel Barry <vgr255 at live.ca> wrote:
> Hello,
>
> You’re presumably doing something like ‘star = starcaller(f); star((“foo”,
> “bar”, “baz”))’ – how is it different from ‘f(*(“foo”, “bar”, “baz”))’ ? I
> don’t see any difference…
Similar to functools.partial, it lets you more easily separate the
process of changing a function's signature from actually calling it.
Compare:
def f(a, b):
print(a, b)
accepts_only_b = functools.partial(f, "predefined_a")
accepts_args_as_tuple = operator.starcaller(f)
>From the point of view of subsequent code, "args_as_tuple" now works
as a callable that accepts a 2-tuple and prints the elements.
However, the similarity to partial does make me wonder whether we
might be better off resurrecting the old apply() builtin as a
functools function:
https://docs.python.org/2/library/functions.html#apply
Then the requested operation would just be:
accepts_args_as_tuple = functools.partial(functools.apply, f)
(with the added bonus of also accepting an optional keyword dict)
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list