Implementing __eq__ using numpy isclose
<p>I fear this might be closed as being a soft question,
but perhaps there is an obvious idiomatic way.</p>

<p>I have a class that contains a lot of information stored in floating point numbers. 
I am thinking about implementing the <code>__eq__</code> method using not exact but numerical equivalence similar to <code>np.isclose</code>. At the moment I have three options:</p>

<ol>
<li><code>__eq__</code> means exact comparison. But there is a function similar to <code>np.isclose</code></li>
<li><code>__eq__</code> means exact comparison. But there is a function similar to <code>np.isclose</code>and <code>__eq__</code> prints a warning everytime it is called and refers to using the function.</li>
<li><code>__eq__</code> means numerical comparison.</li>
</ol>

<p>I can not think of any use case where someone would like to do exact floating point comparison with this class. Hence option three is my favourite. 
But I don't want to surprise the user.</p>

<p>One option would be to add a context manager to switch modes:</p>

<pre><code>from contextlib import contextmanager
class MyObject(object):
    _use_loose_equality = False
    @contextmanager
    @classmethod
    def loose_equality(cls, enabled=True):
        old_mode = cls._use_loose_equality
        cls._use_loose_equality = enabled
        try:
            yield
        finally:
            cls._use_loose_equality = old_mode

    def __eq__(self):
        if self._use_loose_equality:
            ...
        else:
            ...
</code></pre>

<p>Which you would use as:</p>

<pre><code>x = MyObject(1.1)
y = MyObject(1.100001)
assert x != y
with MyObject.loose_equality():
    assert x == y
</code></pre>

<p>Of course, this is still as dangerous as 3, but at least you now have control of when to enable the dangerous behaviour</p>

python python-3.x numpy floating-point precision