LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-18-2013, 02:56 PM   #1
0x53h
LQ Newbie
 
Registered: May 2012
Posts: 23

Rep: Reputation: Disabled
Python object comparison


I have a class that builds a list of objects. I am confused at my inability to identify objects within a list by value. I kind of get I have two separate instances and the instance I am looking for in the list doesn't exist.

So whats the best way to find an object in a list of objects by value?

Code:
class simple():
    def __init__(self, a, b):
        self.a = a
        self.b = b
    
    def __str__(self):
        return str(self.a) + ":" + str(self.b)
        
class abstract():
    
    def __init__(self):
        self.l = [
            simple(1, 2),
            simple(2, 3)   
        ]
        
    def __iter__(self):
        return iter(self.l)
        
if __name__ == "__main__":
    a1 = abstract()
    s1 = simple(1, 2)
    
    print(a1.l[0] in a1.l)          # True
    print(s1 in a1)                 # False
    print(s1 in a1.l)               # False
    print(s1 == a1.l[0])            # False
    print(str(s1), str(a1.l[0]))    # Object values are equal, but found
 
Old 12-19-2013, 03:47 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
add to class simple:
Code:
    def __eq__(self, other):
        return ( (self.a == other.a) & (self.b == other.b) )
 
1 members found this post helpful.
Old 12-19-2013, 11:04 AM   #3
0x53h
LQ Newbie
 
Registered: May 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
Thanks, that was easy. Maddeningly easy.
 
Old 12-19-2013, 01:26 PM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
glad to help you.
remember, by default only the objects were compared and they were different instances (containing the same data).
If you really want to say thanks just press YES
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python - stop an object being destroyed? JohnGraham Programming 2 11-28-2010 03:04 PM
Python: How can I add a method to an object at runtime? cantab Programming 8 11-02-2009 12:37 PM
LXer: Object Oriented Programming in Python LXer Syndicated Linux News 0 01-07-2009 06:20 AM
LXer: Comparison of Python virtual machines LXer Syndicated Linux News 0 11-09-2008 05:00 PM
LXer: How to use GTK+ in C and a C# Python comparison LXer Syndicated Linux News 0 01-12-2006 12:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration