Re: Newbie Question - Overloading ==
- From: Jean-Paul Calderone <exarkun@xxxxxxxxxx>
- Date: Mon, 31 Mar 2008 12:41:32 -0500
On Mon, 31 Mar 2008 10:23:24 -0700 (PDT), xkenneth <xkenneth@xxxxxxxxx> wrote:
So i generally write quite a few classes, and in most I need to
overload the == operator.
If i have two classes, like below:
Class A:
attribute a
attribute b
Class B:
attribute a
attribute c
So if I've overloaded their respective __eq__ functions, and I want to
test whether or not the individual classes attributes are equal, the
code might look something like this:
class A:
def __eq__(self,other):
return self.a == other.a and self.b == other.b
class B:
def __eq__(self,other):
return self.a == other.a and self.c == other.c
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.
Give this a look:
http://jcalderone.livejournal.com/32837.html
Jean-Paul
.
- References:
- Newbie Question - Overloading ==
- From: xkenneth
- Newbie Question - Overloading ==
- Prev by Date: Re: python scripts to standalone executable
- Next by Date: Re: [OT] troll poll
- Previous by thread: Re: Newbie Question - Overloading ==
- Next by thread: Re: Newbie Question - Overloading ==
- Index(es):