Python Forum
How to check if class instance exists in a list of class instance objects?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if class instance exists in a list of class instance objects?
#18
(May-26-2018, 07:31 PM)volcano63 Wrote: AFAIK, every class in Python inherits from object - which is pretty standard. I presume that one can define a class that is not hashable - class, not instance - but i will take some hacking. So I think that your warning is of little merit.

class Person:
    def __init__(self, name):
        self.name =  name

person1 = Person('John')
person2 = Person('John')

persons = [person1, person2]
print(set(persons))
In python3 and python2 the output will be
Output:
{<__main__.Person object at 0x7ff9a43b6828>, <__main__.Person object at 0x7ff9a43b6860>}
so virtually, OP cannot do what they want with set

if there is __eq__() implemented
class Person:
    def __init__(self, name):
        self.name =  name

    def __eq__(self, other):
        return self.name == other.name

person1 = Person('John')
person2 = Person('John')

persons = [person1, person2]
print(set(persons))
in python2
Error:
Traceback (most recent call last): File "/home/boyan/Desktop/f.py", line 12, in <module> print(set(persons)) TypeError: unhashable instance
in python3:
Error:
File "/home/boyan/Desktop/f.py", line 12, in <module> print(set(persons)) TypeError: unhashable type: 'Person'
again, not possible to use set
Also note that even some 'standard' classes like list are not hashable
list1 = [1,2,3]
list2 = [5,6,7]
print(set([list1,list2]))
Error:
Traceback (most recent call last): File "/home/boyan/Desktop/f.py", line 3, in <module> print(set([list1,list2])) TypeError: unhashable type: 'list'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: How to check if class instance exists in a list of class instance objects? - by buran - May-26-2018, 07:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class and methods ebn852_pan 15 730 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 289 May-13-2024, 05:57 AM
Last Post: Larz60+
  Class and methods Saida2024 2 282 May-13-2024, 04:04 AM
Last Post: deanhystad
  How does this code create a class? Pedroski55 6 662 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 351 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 402 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 491 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Help with to check an Input list data with a data read from an external source sacharyya 3 550 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Operation result class SirDonkey 6 655 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 846 Feb-19-2024, 03:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020