Newbie Question - Overloading ==

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Mon Mar 31 15:30:38 EDT 2008


On 31 mar, 20:09, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
> xkenneth <xkenn... at gmail.com> wrote:
> > Now obviously, if I test an instance of either class equal to each
> > other, an attribute error will be thrown, how do I handle this? I
> > could rewrite every __eq__ function and catch attribute errors, but
> > that's tedious, and seemingly unpythonic. Also, I don't want an
> > attribute error thrown whenever two classes are compared that don't
> > have the same attributes.
>
> > I have a sneaky feeling I'm doing something completely unpythonic
> > here.
>
> Surely an A isn't equal to every other object which just happens to have
> the same attributes 'a' and 'b'?

And why not ?-)

> I would have thoughts the tests want to be
> something like:
>
> class A:
>     def __eq__(self,other):
>          return (isinstance(other, A) and
>             self.a == other.a and self.b == other.b)
>
> (and similar for B) with either an isinstance or exact match required for
> the type.

I don't think there's a clear rule here. Python is dynamically typed
for good reasons, and MHO is that you should not fight against this
unless you have equally good reasons to do so.



More information about the Python-list mailing list