[Python-ideas] Infix functions
Mathias Panzenböck
grosser.meister.morti at gmx.net
Sat Feb 22 02:45:30 CET 2014
Am 2014-02-21 23:05, schrieb Andrew Barnert:
> While we're discussing crazy ideas inspired by a combination of a long-abandoned PEP and Haskell idioms (see the implicit lambda thread), here's another: arbitrary infix operators:
>
> a `foo` b == foo(a, b)
>
If you really want to you could do this:
class infix(object):
__slots__ = '__call__',
def __init__(self,func):
self.__call__ = func
def __ror__(self,arg1):
return infix2(self.__call__, arg1)
class infix2(object):
__slots__ = 'func', 'arg1'
def __init__(self,func,arg1):
self.func = func
self.arg1 = arg1
def __call__(*args,**kwargs):
self, args = args[0], args[1:]
return self.func(self.arg1,*args,**kwargs)
def __or__(self,arg2):
return self.func(self.arg1,arg2)
@infix
def foo(a,b):
return a + b
print "egg" |foo| "spam"
More information about the Python-ideas
mailing list