473,795 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

instance comparison


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
7 1693
On Jul 24, 6:50*pm, King <animator...@gm ail.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
__twounderscore s__ 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
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

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
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
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
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__.Spa m instance at 0xb7e8faac>
>>print a
an object
>>b = Spam("an object")
b
<__main__.Spa m instance at 0xb7e8fb6c>
>>print b
an object
>>a == b
False
>>a is b
False
>>str(a) == str(b)
True

</F>

Jul 24 '08 #7

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3339
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this instance!
1
2078
by: Robert Mark Bram | last post by:
Howdy All! I am trying to write a very brief comparison of the Date and Math objects in terms of instance v static objects. What I have below is my best so far. Any criticisms or suggestions are most welcome! Date is an instance object. You use Date by creating instances of it - you call methods on those instances. - you change data to do with each instance.
0
1910
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS Remoting and possible others. Keywords: VSS Remote Access, VSS Web Access, VSS Internet Access,
5
5693
by: mayamorning123 | last post by:
A comparison among six VSS remote tools including SourceOffSite , SourceAnyWhere, VSS Connect, SourceXT, VSS Remoting, VSS.NET To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere 4.0, SourceOffSite 4.1, VSS Connect 1.5, SourceXT 2.1, VSS Remoting 2.5,
17
8242
by: bengamin | last post by:
Hi, I have a C# class and two instance of the class; the class have some property. I want to compare the property value of the two instance How should i do? override == ? use delegate ?
0
1424
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS Remoting and possible others. Keywords: VSS Remote Access, VSS Web Access, VSS Internet Access,
37
2820
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current behaviour, which is returning a value which is, really, id({}) < id(). He also said that default equality comparison will continue to be identity-based. This means that x == y will never raise an exception, as is the situation is now. Here's his reason:
5
1481
by: Russell Warren | last post by:
I just ran across a case which seems like an odd exception to either what I understand as the "normal" variable lookup scheme in an instance/object heirarchy, or to the rules regarding variable usage before creation. Check this out: >>> class foo(object): .... I = 1 .... def __init__(self): .... print self.__dict__ .... self.I += 1
1
6322
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against a software buffer of size 3MB. This is needed so as to see which function to use to read from the file. As the files used range from very large (>30GB) to very small (<3MB), I have enabled large file support and I obtain the file size by using the...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10435
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9037
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6779
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.