Is there a way to check if two object contain the same values in each of their variables in python?

后端 未结 4 1886
日久生厌
日久生厌 2020-12-14 17:29

How do I check if two instances of a

class FooBar(object):
    __init__(self, param):
        self.param = param
        self.param_2 = self.function_2(param         


        
4条回答
  •  既然无缘
    2020-12-14 17:53

    If you want the == to work, then implement the __eq__ method in your class to perform the rich comparison.

    If all you want to do is compare the equality of all attributes, you can do that succinctly by comparison of __dict__ in each object:

    class MyClass:
    
        def __eq__(self, other) : 
            return self.__dict__ == other.__dict__
    

提交回复
热议问题