Created
April 1, 2010 18:07
-
-
Save anfedorov/352159 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BinAccum(object): | |
def __init__(self): | |
self.number = 0 | |
self.place = 0 | |
def __neg__(self): | |
self.place += 1 | |
return self | |
def __pos__(self): | |
self.number += 2**self.place | |
self.place += 1 | |
return self | |
class BinLitaral(object): | |
def __neg__(self): | |
return -BinAccum() | |
def __pos__(self): | |
return +BinAccum() | |
def __sub__(self, other): | |
if isinstance(other, BinAccum): | |
return (-other).number | |
else: | |
return 0 | |
def __add__(self, other): | |
if isinstance(other, BinAccum): | |
return (+other).number | |
else: | |
return 1 | |
i = BinLitaral() | |
if __name__ == "__main__": | |
assert i+-+-+-i == 42 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment