[Python-ideas] Comparable exceptions

Ionel Cristian Mărieș contact at ionelmc.ro
Wed Feb 25 01:01:30 CET 2015


Yes i can compare args but that becomes extremely inconvenient whe there's
a hide data structure and the exception instances are deep down.

What other problems would there be if we add __eq__ on Exception?

User exceptions would be free to define their own __eq__.

On Wednesday, February 25, 2015, Guido van Rossum <guido at python.org> wrote:

> Actually, they don't have a __hash__ either -- they just inherit the
> default __hash__ from object, just as they inherit __eq__ from object (and
> both just use the address of the object). I can see many concerns with
> trying to define __eq__ for exceptions in general -- often there are a
> variety of fields, not all of which perhaps should be considered when
> comparing, and then there are user-defined exceptions which may have other
> attributes. I don't know what your use case was, but if, as you say, the
> exceptions you care about already have `args` attributes, maybe you should
> just compare that (and the type).
>
> On Tue, Feb 24, 2015 at 3:43 PM, Ionel Cristian Mărieș <contact at ionelmc.ro
> <javascript:_e(%7B%7D,'cvml','contact at ionelmc.ro');>> wrote:
>
>> Hey everyone,
>>
>> I was writing a test the other day and I wanted to assert that some
>> function raises some exception, but with some exact value. Turns out you
>> cannot compare OSError("foobar") to OSError("foobar") as it doesn't have
>> any __eq__. (it's the same for all exceptions?)
>>
>> I think exceptions are great candidates to have an __eq__ method - they
>> already have a __hash__ (that's computed from the contained values) and
>> they are already containers in a way (they all have the `args` attribute).
>>
>> Comparing exceptions in a test assertion seems like a good usecase for
>> this.
>>
>> Thanks,
>> -- Ionel Cristian Mărieș, blog.ionelmc.ro
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> <javascript:_e(%7B%7D,'cvml','Python-ideas at python.org');>
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
>
> --
> --Guido van Rossum (python.org/~guido)
>


-- 

Thanks,
-- Ionel Cristian Mărieș, blog.ionelmc.ro
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150225/b08e728e/attachment-0001.html>


More information about the Python-ideas mailing list