By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,385 Members | 1,119 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,385 IT Pros & Developers. It's quick & easy.

instance comparison

P: n/a

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__ or else it will comapre whether they are the
same instances?
Jul 24 '08 #1
Share this Question
Share on Google+
7 Replies

P: n/a
On Jul 24, 6:50*pm, King <animator...@gmail.comwrote:
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.
Jul 24 '08 #2

P: n/a
King wrote:
Is this mean when you have overridden __str__ method then it comapre
with results of __str__ or else it will comapre whether they are the
same instances?
Comparisons uses __cmp__ or the rich comparison set; see

http://docs.python.org/ref/customization.html

for details.

Sets and dictionaries, also relies on hashing (the __hash__ method, also
described on the linked page).

The __str__ method only affects printing and conversion to string.

</F>

Jul 24 '08 #3

P: n/a

The only methods I do have in class is __init__ and __str__.
How ever inst1 and inst2 is coming from a dictionary where I stored
them with a unique id.

inst1 = stored[id]
inst2 = stored[id]

Is this makes a difference? I will rip down the piece of code giving
me problem and post.
Jul 24 '08 #4

P: n/a
King wrote:
The only methods I do have in class is __init__ and __str__.
How ever inst1 and inst2 is coming from a dictionary where I stored
them with a unique id.

inst1 = stored[id]
inst2 = stored[id]

Is this makes a difference?
unlikely (well, if that's the literal code, both variables will point to
the same variable, but I guess that's not what you meant).
I will rip down the piece of code giving me problem and post.
please do. and don't be surprised if you find the problem while doing
so ;-)

</F>

Jul 24 '08 #5

P: n/a
No,

The the class is not subclass of another one. Problem still persist.
The code is pretty huge and I am trying to post the information as
clear as possible.
Jul 24 '08 #6

P: n/a
King skrev:
The the class is not subclass of another one. Problem still persist.
The code is pretty huge and I am trying to post the information as
clear as possible.
feel free to *add* stuff to the following example until it breaks, if
that's easier:
>>class Spam:
.... def __init__(self, label):
.... self.label = label
.... def __str__(self):
.... return self.label
....
>>a = Spam("an object")
a
<__main__.Spam instance at 0xb7e8faac>
>>print a
an object
>>b = Spam("an object")
b
<__main__.Spam instance at 0xb7e8fb6c>
>>print b
an object
>>a == b
False
>>a is b
False
>>str(a) == str(b)
True

</F>

Jul 24 '08 #7

P: n/a

On Jul 24, 2008, at 7:53 PM, King wrote:
The the class is not subclass of another one. Problem still persist.
The code is pretty huge and I am trying to post the information as
clear as possible.
Mark V. Shaney, from Dissociated Press, I presume?

--
PA.
http://alt.textdrive.com/nanoki/
Jul 24 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.