Ask Your Question

Revision history [back]

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

Delayed evaluation without all the parenthesis?

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

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

gives

(x + x) + x x

not x + x + x

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/384122-infix-operators/.

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