How to perform approximate structural pattern matching for floats and complex

The key to the solution is to build a wrapper that overrides the __eq__ method and replaces it with an approximate match: import cmath class Approximately(complex): def __new__(cls, x, /, **kwargs): result = complex.__new__(cls, x) result.kwargs = kwargs return result def __eq__(self, other): try: return isclose(self, other, **self.kwargs) except TypeError: return NotImplemented It creates approximate … Read more