vars(self) == vars(other)
, it seems too easy
I implemented a class
1 2 3 4 |
|
Then when I created two equivalent instances and compared them, I got false.
1 2 3 4 5 6 7 8 |
|
This must be because the ==
operator performs reference equality by default
which is what I should have expected, so I implemented a simple equivalence operator
1 2 3 |
|
Then I figured there must be an easier way to do something so simple. So I
looked for what reflection methods are available on my new class, and
found the method vars(object)
, which for this thing would return
1
|
|
This is just what I needed for a very simple equivalence operator
1 2 |
|
Unfortunately, vars(a)
doesn’t just work on everything:
1 2 3 4 |
|
So I’m assuming vars(a)
just calls a.__dict__
.
I looked on Google and GitHub and didn’t find too many examples of people doing this, but one example said
1 2 3 |
|
So, until I find a reason not to, my default equality implementation shall be
1 2 3 |
|
Assuming this is semantically what is desired of class equality, is there any reason this is a Bad Way to implement it? Why has no one told me about this? It seems too easy, but I guess sometimes things are just simple.