lazy arithmetic

pianomaestro at gmail.com pianomaestro at gmail.com
Fri Aug 25 01:18:16 EDT 2006


Gabriel Genellina wrote:
> At Friday 25/8/2006 00:36, pianomaestro at gmail.com 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 until now that's the way
I always did it. Many Many times.
Now I am looking for a way to build ensembles of these lazy classes at
the meta-level.
(think abstract algebra).

> >Item.__add__ = Add
>
> This doesn't make sense... __add__ should be a method, not a class...

No, that's not true. It can be this callable:

def add(a, b):
  return Add(a,b)
Item.__add__ = add

So my thinking was this: why can't I use a callable class ?

> 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)

Simon.




More information about the Python-list mailing list