Skip to content

Instantly share code, notes, and snippets.

@anfedorov
Created April 1, 2010 18:07
Show Gist options
  • Save anfedorov/352159 to your computer and use it in GitHub Desktop.
Save anfedorov/352159 to your computer and use it in GitHub Desktop.
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