473,396 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

test whether 2 objects are equal

Hello,
I need to compare 2 instances of objects to see whether they are equal
or not, but with the code down it does not work (it outputs "not equal")
#!/usr/bin/python

class Test:
var1 = ''
var2 = ''

test1 = Test()
test1.var1 = 'a'
test1.var2 = 'b'

test2 = Test()
test2.var1 = 'a'
test2.var2 = 'b'

if test1 == test2:
print "equal"
else:
print "not equal"


What am I doing wrong...?
best regards,
Yves
Jan 31 '06 #1
5 12355
Yves Glodt:
I need to compare 2 instances of objects to see whether they are equal
or not,


This prints "equal":

class Test(object):
def __init__(self):
self.var1 = ''
self.var2 = ''
def __eq__(self,other):
return self.var1 == other.var1 and self.var2 == other.var2

test1 = Test()
test1.var1 = 'a'
test1.var2 = 'b'

test2 = Test()
test2.var1 = 'a'
test2.var2 = 'b'

if test1 == test2:
print "equal"
else:
print "not equal"

--
René Pijlman
Jan 31 '06 #2
Rene Pijlman wrote:
Yves Glodt:
I need to compare 2 instances of objects to see whether they are equal
or not,
This prints "equal":


thank you!

Have a nice day,
Yves
class Test(object):
def __init__(self):
self.var1 = ''
self.var2 = ''
def __eq__(self,other):
return self.var1 == other.var1 and self.var2 == other.var2

test1 = Test()
test1.var1 = 'a'
test1.var2 = 'b'

test2 = Test()
test2.var1 = 'a'
test2.var2 = 'b'

if test1 == test2:
print "equal"
else:
print "not equal"

Jan 31 '06 #3
Yves Glodt wrote:
Hello,
I need to compare 2 instances of objects to see whether they are equal
or not, but with the code down it does not work (it outputs "not equal")
#!/usr/bin/python

class Test:
var1 = ''
var2 = ''
Take care, this creates two *class* variables var1 and var2. For
*instance* variables, you want:

class Test:
def __init__(self, var1='', var2=''):
self.var1 = var1
self.var2 = var2

test1 = Test()
test1.var1 = 'a'
test1.var2 = 'b'


This creates instances variables var1 and var2 for test1 (shadowing
class variables).

(snip the rest, see other posts in this thread)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jan 31 '06 #4
bruno at modulix wrote:
Yves Glodt wrote:
Hello,
I need to compare 2 instances of objects to see whether they are equal
or not, but with the code down it does not work (it outputs "not equal")
#!/usr/bin/python

class Test:
var1 = ''
var2 = ''
Take care, this creates two *class* variables var1 and var2. For
*instance* variables, you want:


Thanks for making me aware. I'll have to read more about classes in
python... ( As you can see I'm still new to it ;-)

btw, this is the best list I've ever joined, very helpful and nice ppl.

Have a nice day!
Yves
class Test:
def __init__(self, var1='', var2=''):
self.var1 = var1
self.var2 = var2

test1 = Test()
test1.var1 = 'a'
test1.var2 = 'b'


This creates instances variables var1 and var2 for test1 (shadowing
class variables).

(snip the rest, see other posts in this thread)

Jan 31 '06 #5
Yves Glodt a écrit :
bruno at modulix wrote:
Yves Glodt wrote:
(snip)
#!/usr/bin/python

class Test:
var1 = ''
var2 = ''

Take care, this creates two *class* variables var1 and var2. For
*instance* variables, you want:

Thanks for making me aware. I'll have to read more about classes in
python... ( As you can see I'm still new to it ;-)


I don't remember what's your background, but if you come from
C++/Java/..., then yes, definitevely, you need to learn more about
Python's object model. The class variable/instance variable confusion is
a common gotcha.

BTW, better use 'new-style' classes (that is - for short-, classes that
inherit at least from 'object' or any other new-style class), ie:

class NewStyle(object):
# this is a new-style class
pass

class NewStyleToo(NewStyle):
# this is a new-style class too
pass

class OldStyle:
# this is a deprecated old-style class

btw, this is the best list I've ever joined, very helpful and nice ppl.


Yes, this is definitively a nice place !-)
Feb 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Nate Bargmann | last post by:
I am working on a function that takes degrees, minutes, seconds coordinates and converts them to decimal representation. Traditionally, in DMS notation the '-' sign, to indicate west longitude or...
2
by: Ed Sutton | last post by:
I have variables that are declared as objects. Ints, strings and other data types are then assigned to these objects. How can I write a compare objects function? I tried objOne==objTwo. I am...
34
by: Andrew | last post by:
Is there anyway to test if a pointer points to allocated memory or not? For example if I have a pointer such as char *p is there a standard way to test whether an assignment such as the following...
67
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
10
by: Christian Blackburn | last post by:
Hi Gang, Sometimes when I try to open a registry it doesn't exist and therefore the object reference is equal to nothing. However VB.NET 2003 doesn't allow the syntax if If objReg <> Nothing...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
14
by: ThazKool | last post by:
I want to see if this code works the way it should on a Big-Endian system. Also if anyone has any ideas on how determine this at compile-time so that I use the right decoding or encoding...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
6
by: lex | last post by:
Of course there is the always the iteration method: list = status = True for each in list: status = status and each but what is your best way to test for for False in a list?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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,...

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.