Re: lazy arithmetic

2006-08-25 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote: > Simon Forman wrote: > > But that's a function, not a class. When you assign add to an > > attribute of Item it "magically" becomes a method of Item: > > Yes, I am looking to understand this magic. > Sounds like I need to dig into these descriptor thingies (again). >

Re: lazy arithmetic

2006-08-25 Thread pianomaestro
Simon Forman wrote: > But that's a function, not a class. When you assign add to an > attribute of Item it "magically" becomes a method of Item: > Yes, I am looking to understand this magic. Sounds like I need to dig into these descriptor thingies (again). (sound of brain exploding).. Simon.

Re: lazy arithmetic

2006-08-25 Thread Peter Otten
[EMAIL PROTECTED] wrote: > # 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 abso

Re: lazy arithmetic

2006-08-24 Thread Gabriel G
At Friday 25/8/2006 02:18, [EMAIL PROTECTED] wrote: > x+y get translated to x.__add__(y) No that's not true at all. The self argument to __add__ ends up being the Add instance, not the Item instance: z=x+y is translated to z.__add__(y) Well, I deleted my response after I noticed that Simon

Re: lazy arithmetic

2006-08-24 Thread Simon Forman
[EMAIL PROTECTED] wrote: > Simon Forman wrote: > > > > > "Item.__add__ = Add" is a very strange thing to do, I'm not surprised > > it didn't work. > > Yes it is strange. > I also tried this even stranger thing: > > class Item(object): > class __add__(object): > def __init__(self, a, b=None):

Re: lazy arithmetic

2006-08-24 Thread Simon Forman
[EMAIL PROTECTED] wrote: > Gabriel Genellina wrote: > > At Friday 25/8/2006 00:36, [EMAIL PROTECTED] wrote: > > > > ># This is what I have in mind: > > > > > >class Item(object): > > > def __add__(self, other): > > > return Add(self, other) > > > > And this works fine... why make thinks compl

Re: lazy arithmetic

2006-08-24 Thread pianomaestro
Simon Forman wrote: > > "Item.__add__ = Add" is a very strange thing to do, I'm not surprised > it didn't work. Yes it is strange. I also tried this even stranger thing: class Item(object): class __add__(object): def __init__(self, a, b=None): print self, a, b self.a = a

Re: lazy arithmetic

2006-08-24 Thread pianomaestro
Gabriel Genellina wrote: > At Friday 25/8/2006 00:36, [EMAIL PROTECTED] wrote: > > ># This is what I have in mind: > > > >class Item(object): > > def __add__(self, other): > > return Add(self, other) > > And this works fine... why make thinks complicated? Yes, I agree it's simpler, and up u

Re: lazy arithmetic

2006-08-24 Thread Simon Forman
[EMAIL PROTECTED] wrote: > # 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

Re: lazy arithmetic

2006-08-24 Thread Gabriel Genellina
At Friday 25/8/2006 00:36, [EMAIL PROTECTED] wrote: # This is what I have in mind: class Item(object): def __add__(self, other): return Add(self, other) And this works fine... why make thinks complicated? # Now, I am going absolutely crazy with this idea # and using it in a big way. S

lazy arithmetic

2006-08-24 Thread pianomaestro
# 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