<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2963" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>
<DIV style="BACKGROUND-COLOR: #ffffff">
<DIV><FONT face=Arial size=2>Consider the following code:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>import sys</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>class FirstClass:<BR> def
__init__(self, value):<BR> self.data =
value<BR> def __add__(self,
value):<BR> return
FirstClass(self.data + value)<BR> def
display(self):<BR> print
self.data</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>class
SecondClass(FirstClass):<BR> def __add__(self,
value):<BR> # Generalized version of
SecondClass(self.data + value).<BR>
sum = FirstClass(self.data) +
value<BR> return
SecondClass(sum.data)<BR> def
display2(self):<BR> print
self.data</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>x = SecondClass(1)<BR># Must reimplement __add__ in
SecondClass or else x after the assignment is<BR># an instance of class
FirstClass not SecondClass. This implies that all<BR># subclasses of
classes that overload operators such as + must reimplement<BR># those methods to
ensure that the class being returned is correct.<BR>x += 1
<BR>x.display2()<BR></FONT></DIV>
<DIV><FONT face=Arial size=2>Note the comments in the code and observe that
as is execution yields:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>C:\Files\Python> problem.py<BR>2</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>But if the reimplementation of __add__ in
SecondClass is removed (or commented out) then as claimed in the </FONT><FONT
face=Arial size=2>comment above the code does not work:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>C:\Files\Python> problem.py<BR>Traceback (most
recent call last):<BR> File "C:\Files\Python\problem.py", line 25, in
?<BR> x.display2()<BR>AttributeError: FirstClass instance has
no attribute 'display2'</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>This is because x after the statement x += 1 has
been executed is an object of class FirstClass because the method implementing
addition in FirstClass explicitly returns a FirstClass object.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>This seems to me to be a big problem because what
this means is that if class X overloads any operator, e.g. addition,
then all subclass of X (and all subclasses of those subclasses
recursively)</FONT></DIV>
<DIV><FONT face=Arial size=2>must reimplement all the operator methods
defined in X.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Is this a real problem with Python or just the way
I am coding it as I admit that I am new to Python?</FONT></DIV>
<DIV><FONT face=Arial size=2>If its my fault then what is the correct way to
implement SecondClass?</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Thanks,</FONT></DIV>
<DIV><FONT face=Arial size=2>Edward</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial><FONT size=2>P.S. Why were slices ([low:high]) implemented
to return the low'th to high-1'th values of a sequence? For example I
would expect that [2:4] of "abcdef" would return "cde" not "cd" and this is the
case in other languages that do support slices of strings (HP BASIC) and arrays
(Perl). But I do accept that this a personal preference of mine so if
folks don't agree with me that is fine.</FONT></FONT></DIV>
<DIV><FONT color=#000000> </DIV></DIV></FONT></FONT></DIV></BODY></HTML>