add object into python's set collection and determine by object's attribute

后端 未结 1 950
孤街浪徒
孤街浪徒 2020-12-10 06:29

I have a Person class like this:

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

    def __rep         


        
相关标签:
1条回答
  • 2020-12-10 06:35

    When a new object is being added to a python set, the hash code of the object is first computed and then, if one or more objects with the same hash code is/are already in the set, these objects are tested for equality with the new object.

    The upshot of this is that you need to implement the __hash__(...) and __eq__(...) methods on your class. For example:

    class Person:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        def __eq__(self, other):
            return self.age == other.age
    
        def __hash__(self):
            return hash(self.age)
    
        def __repr__(self):
            return '<Person {}>'.format(self.name)
    
    tom = Person('tom', 18)
    mary = Person('mary', 22)
    mary2 = Person('mary2', 22)
    
    person_set = {tom, mary, mary2}
    print(person_set)
    # output: {<Person tom>, <Person mary>}
    

    However, you should think very carefully about what the correct implementation of __hash__ and __eq__ should be for your class. The above example works, but is non-sensical (e.g. in that both __hash__ and __eq__ are defined only in terms of age).

    0 讨论(0)
提交回复
热议问题