This class should be put somewhere. If you need it from inside a method,
you have to choose where to put it. If it is inside the method, it forces
the reader to scan it thoroughly to verify there's no "real" closure there,
and it also makes your methods significantly longer. If it is outside the
whole class, it might be very far away from your logic, doing a very simple
thing that is not needed anywhere else.
Elazar
On Wed, Sep 21, 2016 at 5:32 AM Dan Sommers
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).
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/