newbie class-building question

Jon Clements joncle at googlemail.com
Thu Nov 9 09:12:55 EST 2006


jrpfinch wrote:

> Thank you this is very helpful.  The only thing I now don't understand
> is why it is calling __coerce__.  self.wrapped and other are both
> lists.

Yes, but in "a + [5]", *a* is a myListSub object -- it's not a list! So
__coerce__ is called to try and get a common type...

Try this in myList...

# We could  check the type of 'other' to determine what we return
here....
# At the moment, we return a list, whose + operator, requires another
list so this works
# and gives an exception if it's not for us
def __coerce__(self,other):
    return self.wrapped, other

# 
def __add__(self,other):
    return self + other



hth

Jon.




More information about the Python-list mailing list