Using 'set' on custom objects

PHOTO EMBED

Mon Apr 18 2022 17:47:28 GMT+0000 (Coordinated Universal Time)

Saved by @taha #python

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

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

    def __repr__(self):
        return f"MyClass({self.name}, {self.age})"

    def __hash__(self):
        return hash(self.__repr__())


a = MyClass('taha', 30)
b = MyClass('taha', 30)

c = {a, b}
print(c)
content_copyCOPY