[Python-ideas] Infix application of binary functions (original) (raw)

Carl M. Johnson cmjohnson.mailinglist at gmail.com
Thu Jul 22 05:49:16 CEST 2010


Thought about it some more. Here’s a more general formula:

class InfixArity(object): def init(self, arity): self.arity = arity self.args = []

def __call__(self, func):
    self.func = func
    return self

def __add__(self, arg):
    self.args.append(arg)
    if len(self.args) < self.arity:
        return self
    else:
        return self.func(*self.args)

__radd__ = __add__

Infix = lambda func: InfixArity(2)(func)

And of course, one can use mul or div or whatever to taste. "1 // add // 2” doesn’t make me instantly vomit in my mouth. ;-)

-- Carl Johnson



More information about the Python-ideas mailing list