[Python-ideas] Make partial a built-in
Dan Sommers
dan at tombstonezero.net
Tue Sep 20 22:31:18 EDT 2016
On Tue, 20 Sep 2016 15:29:36 +0000, אלעזר wrote:
> The alternative to partial is writing a closure in the form of a
> function, that needs to be carefully inspected to verify that it is
> indeed just a partial application and not something more complex. It
> has more opportunity for introducing an error. And it's longer and
> adds distance between related parts of the code.
While I'm not usually one to promote object oriented programming,
another Python alternative is a class with a __call__ method; e.g.:
class Adder:
def __init__(self, addend_one):
self.addend_one = addend_one
def __call__(self, addend_two):
return self.addend_one + addend_two
add_5 = Adder(5)
print(add_5(4))
I like the way the whole thing is bundled up into a class. Yes, it's
more verbose than a lambda expression or a partial function application,
but I find it very readable and its intent is usually pretty obvious.
Instances are closures in disguise (and they're all just different ways
of hiding state of one kind or another).
More information about the Python-ideas
mailing list