Closed maxpeng closed 3 years ago
From python - Compare object instances for equality by their attributes - Stack Overflow, this should be better:
class Event(object):
...
def __eq__(self, other):
if isinstance(other, Event):
return self.signal == other.signal and self.value == other.value
return False
On one hand, I like this implementation because it is concise. On the other hand, I don't feel it is necessary; comparing signals should be sufficient. Can you tell me a situation or use case where comparing the values is important?
Assume the state machine forwards the received event to a handler. When we perform the unit testing, we can use a mock as the handler and assert the mock was called with given event. This requires the Event clas implement the __eq__
method.
When I perform unit testing for the python code using
farc
, I may need to compare two different Event objects for assertion. So I propose to add__eq__
function toEvent
class for supporting that.