Problems with classes and __add__ and __mul__...

Helmut Weil weil at sireconnect.de
Thu Jun 15 20:38:44 EDT 2000


<abg at saturnsys.com> schrieb in im Newsbeitrag:
8ibmn8$k5h$1 at nnrp1.deja.com...
> Hi all,
>    I'm having a little problem with classes right now.  It's a class
> with a list as it 's main feature.  I've done the slices and such so that
> instance[:] and instance[x] work.  However, I can't seem to get
> __add__ and __mul__ to function the way that I want them to.
>

Just do this ...
def __add__(self, other_instance):
    result = spam()
    result.list =  self.list + other_instance.list
    return result

... and this will work as you wanted:
> z = spam()
> x = spam()
> z = z+x

Hope that helps.





More information about the Python-list mailing list