instance comparison

John Machin sjmachin at
Thu Jul 24 11:01:42 CEST 2008

On Jul 24, 6:50 pm, King <animator... at> wrote:
> I am facing a problem where I am really confused about it.
> I am trying to compare to instances using:
> if inst1 == inst2
> These instances have a overridden method __str__ which returns same
> string. The condition result is true although they are different
> instances.
> If I use:
> if id(inst1) == id(inst2)
> The results is false. What's happening here?
> If I am trying to store this pair in a set where other instances are
> already stored, but it's not storing it. Although I am sure that there
> is no pair already stored of same but even if Set is comparing by
> using string return by __str__ method then it won't.
> How do I solve this?
> Is this mean when you have overridden __str__ method then it comapre
> with results of __str__

No. Do you have a __cmp__ method, or an __eq__ method? Any other
__twounderscores__ methods?

It is impossible to tell what is going on from your description.
Please supply the source of a *short* class that demonstrates the
problem, and demonstrate it.

More information about the Python-list mailing list