[Python-ideas] Comparable exceptions

Steven D'Aprano steve at pearwood.info
Wed Feb 25 14:32:46 CET 2015


On Wed, Feb 25, 2015 at 03:03:40PM +0200, Ionel Cristian Mărieș wrote:
> Yes, it does, however my assertion looks similar to this:
> 
>     assert result == [1,2, (3, 4, {"bubu": OSError('foobar')})]

I'm not keen on using assert like that. I think that using assert for 
testing is close to abuse of the statement, and it makes it impossible 
to test your code running with -O.

But regardless of whether that specific test is good practice or not, I 
think it is reasonable for exception instances to have a more useful 
__eq__ than that provided by inheriting from object. Perhaps add 
something like this to BaseException:

    def __eq__(self, other):
        if isinstance(other, type(self)):
            return self.args == other.args
        return NotImplemented




-- 
Steve


More information about the Python-ideas mailing list