Ask Your Question

Alister's profile - activity

2022-03-31 13:31:42 +0200 received badge  Notable Question (source)
2016-10-05 21:18:01 +0200 received badge  Popular Question (source)
2011-11-10 19:09:30 +0200 commented answer Delayed evaluation without all the parenthesis?

Nice! But that still leaves some unnecessary parenthesis.

2011-11-10 16:04:02 +0200 received badge  Editor (source)
2011-11-10 15:48:53 +0200 received badge  Supporter (source)
2011-11-10 04:28:18 +0200 asked a question Delayed evaluation without all the parenthesis?

Is there a way to have delayed evaluation without having unnecessary parenthesis. For example,

(x.add(x, hold = True)).add(x, hold = True)

gives

(x + x) + x

not

x + x + x

Thanks

EDIT:

Great answer burcin! I was hoping to be able to it in more of a binary way though, so that I could use the Infix hack from http://code.activestate.com/recipes/3....

I wanted to make a binary operator which is still delayed. Defined here

class Infix(object):
    def __init__(self, function):
        self.function = function
    def __ror__(self, other):
        return Infix(lambda x: self.function(other, x))
    def __or__(self, other):
        return self.function(other)

p=Infix(lambda x,y: x.add(y, hold=True))

Usage is

x |p| x |p| x