test whether 2 objects are equal

Y

Yves Glodt

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
 
R

Rene Pijlman

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"
 
B

bruno at modulix

Yves said:
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)
 
Y

Yves Glodt

bruno said:
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
 
B

Bruno Desthuilliers

Yves Glodt a écrit :
(snip)



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 !-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top