lazy arithmetic

pianomaestro at gmail.com pianomaestro at gmail.com
Thu Aug 24 23:36:32 EDT 2006


# This is what I have in mind:

class Item(object):
  def __add__(self, other):
    return Add(self, other)

class Add(Item):
  def __init__(self, a, b):
    self.a = a
    self.b = b

a = Item()
b = Item()

c = a+b

# Now, I am going absolutely crazy with this idea
# and using it in a big way. So I'm looking at
# automating the process. As a first step,
# I thought maybe this would work:

class Item(object):
  pass

class Add(Item):
  def __init__(self, a, b=None):
    print self, a, b
    self.a = a
    self.b = b

Item.__add__ = Add

x = Item()
y = Item()

print x, y

c = x+y

# This time, the Add constructor gets only the first two arguments:
"self" and "y".
# So, what happened to "x" ? Is this some kind of property voodoo going
on ?

# Simon.




More information about the Python-list mailing list