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 5 12372
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,oth er):
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
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,oth er): 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"
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('@')])"
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)
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(New Style):
# 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 !-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 north
latitude, precedes the degree value, e.i. -96° 59' 59". The float type
is capable of carrying a signed ZERO for the purpose of representing
between 0 and 1 degree west or south.
My problem comes from trying to select the correct...
|
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
guessing that this must be comparing references and not values because it
returns false.
// Test case
objOne = strName1;
objTwo = strName2;
|
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 has been
applied?
p = (char *) malloc(sizeof(char) * n);
|
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 achieve this would be a linear search through the array:
int ptrInArrSafe(T *p,T *a,int max)
/* check whether p points to an element of a */
|
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 then
'do this
End if
Can somebody tell me another way to check if the key failed to open? I
realize I can just use error handling, but I prefer to make that the last
| |
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 compare these two objects so that it will return true if both
object's members have the same value...
it is good if u can give me a single function or simple code snippet..
Thank U
--
Peter...
|
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 functions, I
would greatly appreciate the help.
Thanks,
Ché
#include <iostream>
|
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 their own unit
test framework, and then in a lab session see if I can get them to
write their own. To give them an example I've created the following
UTF class (with a simple test program following). I would welcome and
suggestions on how anybody...
|
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?
|
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...
|
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,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |