Confused again
Steven Taschuk
staschuk at telusplanet.net
Sun Feb 23 23:36:25 EST 2003
Quoth Duncan Smith:
> I'm still (again) having a little trouble with operator overloading. I
> changed the original code I posted yesterday (Confusing problem) so that it
> now (apparently) does what I want. I simply changed "def __cmp__(self,
> other):" to "def __eq__(self, other):". [...]
This has nothing to do with your problem below, but you probably
also want to provide __ne__. Notice:
>>> class spam(object):
... def __eq__(self, other):
... return 1 # all spams are created equal
...
>>> a = spam()
>>> b = spam()
>>> a == b
1
>>> a != b
1
Users of spam might be confused by this behaviour.
What's happening is that spam has no __ne__, so != falls back to
... an implementation that tests 'is not same object', I think.
(Certainly a != a yields 0.) In any case, it doesn't do what you
want. Adding, for example,
def __ne__(self, other):
return not self == other
will suffice (unless you're using NotImplemented tricks in __eq__).
> [...] But if I try to raise an exception
> after calling the function, the exception is apparently ignored. eg. the
> following code snippet,
>
> print table == numtable2
> if table == numtable2:
> to_remove.append(table)
> else:
> print 'what?'
> raise Error
>
> class Error:
> pass
>
>
> >>> c.clusters[(3,)].pruneTables()
> 0 # evaluates to false as expected
> what? # prints 'what?' OK
> >>> # but no error
Puzzling. What's the rest of pruneTables()?
--
Steven Taschuk o- @
staschuk at telusplanet.net 7O )
" (
More information about the Python-list
mailing list