Since you tagged this with python, I'll give you the Python perspective on this.
In Python, this is entirely normal. Attributes are not private, they are merely marked as 'internal', by convention, by using a leading underscore. So
_store is something that is 'internal' to the class, just as the implementation of
__eq__ is an internal matter.
You are not breaking encapsulation here; you are merely providing a correct implementation of a hook method. Accessing
other._store here is no different from accessing
self._store in that respect. That's because Python is a pragmatic language, it is not a purist OO language (you can use functional and procedural paradigms whenever you feel that fits the problem space better, for example).
Note that you may want to return the
NotImplemented singleton object for comparisons that your class doesn't support:
def __eq__(self, other):
if not isinstance(other, MyClass): # or not hasattr(other, '_store') perhaps
return self._store == other._store
Python would then delegate the test to the
other object; if it doesn't implement the
__eq__ method or returns
NotImplemented as well, then Python falls back to an identity test (
self is other).
You'll find this pattern (using internal attributes in comparison hook methods) throughout the Python standard library. For example, all comparison methods for the
decimal.Decimal() class delegate to the
Decimal._cmp() method, and the implementation for that method is based almost exclusively on using internal attributes and methods (
_int), accessed both on
Some more examples: