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
| 1 | initial version |
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
| 2 | No.2 Revision |
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
Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.