(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 setif 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 setAlso 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
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs