Confused again

Duncan Smith buzzard at urubu.freeserve.co.uk
Mon Feb 24 10:16:21 EST 2003


"Steven Taschuk" <staschuk at telusplanet.net> wrote in message
news:mailman.1046060842.23969.python-list at python.org...
> 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__).
>

Yes.  I wondered about that last night, and used "not table1 == table2"
rather than "table1 != table2" to be on the 'safe' side.  I'll have to add
the above function to my code to tidy it up.

> > [...] 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()?

Puzzling until you know how stupid I can be when I'm writing code in the
early hours.  I was catching the exception!  A complete function is in my
response to Alex's post.  Cheers.

Duncan

>
> --
> Steven Taschuk                                                 o- @
> staschuk at telusplanet.net                                      7O   )
>                                                                "  (
>






More information about the Python-list mailing list