Christopher,<br>
<br>
I copied and pasted your code and it worked only a few
modifications. The thing I had to change was the indention level
of the:<br>
<br>
def __and__(self, other): return self.intersect(other)<br>
def __or__(self, other): return self.union(other)<br>
def __repr__(self): return 'Set:' + list.__repr__(self)<br><br><div><span class="gmail_quote">statements. These statements need to be on the same indention level as your __init__, intersection and union satements.<br>
<br>
Here's what I got:<br>
<br>
class Set(list):<br>
def __init__(self, value=[]):<br>
list.__init__([])<br>
self.concat(value)<br>
<br>
def intersect(self, other):<br>
res = []<br>
for x in self:<br>
if x in other:<br>
res.append(x)<br>
return Set(res)<br>
<br>
def union(self, other):<br>
res = Set(self)<br>
res.concat(other)<br>
return res<br>
<br>
def concat(self, value):<br>
for x in value:<br>
if not x in self:<br>
self.append(x)<br>
<br>
def __and__(self, other): return self.intersect(other)<br>
def __or__(self, other): return self.union(other)<br>
def __repr__(self): return 'Set:' + list.__repr__(self)<br>
<br>
if __name__ == '__main__':<br>
x = Set([1,3,5,7])<br>
y = Set([2,1,4,5,6])<br>
print x, y, len(x)<br>
print x.intersect(y), y.union(x)<br>
print x & y, x | y<br>
x.reverse(); <br>
print x<br>
<br>
On 1/23/06, <b class="gmail_sendername">Christopher Spears</b> <<a href="mailto:cspears2002@yahoo.com">cspears2002@yahoo.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I copied this code from Learning Python while learning<br>about extending types by subclassing:<br><br>class Set(list):<br> def __init__(self, value=[]):<br> list.__init__([])<br> self.concat
(value)<br><br> def intersect(self, other):<br> res = []<br> for x in self:<br> if
x in other:<br> res.append(x)<br> return Set(res)<br><br> def union(self, other):<br> res = Set(self)<br> res.concat(other)<br> return res
<br><br> def concat(self, value):<br> for x in value:<br> if
not x in self:<br> self.append(x)<br><br>def __and__(self, other): return self.intersect(other)<br>def __or__(self, other): return self.union(other)<br>def __repr__(self): return 'Set:' +<br>
list.__repr__(self)<br><br>if __name__ == '__main__':<br> x = Set([1,3,5,7])<br> y = Set([2,1,4,5,6])<br> print x, y, len(x)<br> print x.intersect(y), y.union(x)<br> print x & y, x | y
<br> x.reverse(); print x<br><br>Here is the result:<br><br>cspears@iaws09:/imports/home/cspears/Documents/Python/chap23><br>python setsubclass.py<br>[1, 3, 5, 7] [2, 1, 4, 5, 6] 4<br>[1, 5] [2, 1, 4, 5, 6, 3, 7]
<br>Traceback (most recent call last):<br> File "setsubclass.py", line 32, in ?<br> print x & y, x | y<br>TypeError: unsupported operand type(s) for &: 'Set'<br>and 'Set'<br><br>According to the book, here is what I should get:
<br><br>Set:[1, 3, 5, 7] Set:[2, 1, 4, 5, 6] 4<br>Set:[1, 5] Set:[2, 1, 4, 5, 6, 3, 7]<br>Set:[1, 5] Set:[1, 3, 5, 7, 2, 4, 6]<br>Set:[7, 5, 3, 1]<br><br>Problem 1: Why isn't "Set:" being printed? I thought<br>
<br><br> def __repr__(self): return 'Set:' +<br>list.__repr__(self)<br><br>would facilitate that.<br><br>Problem 2: What is causing the TypeError?<br><br>I'm pretty sure I copied this exactly from the book,<br>so I'm not sure what is not working.
<br>_______________________________________________<br>Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor
</a><br></blockquote></div><br>