[Python-ideas] Comparable exceptions

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


Ok, it looks I got confused about the hash (it's based on the memory
address and I wasn't saving the objects around, it was always the same
address, oops).




Thanks,
-- Ionel Cristian Mărieș, blog.ionelmc.ro

On Wed, Feb 25, 2015 at 1:57 AM, Chris Angelico <rosuav at gmail.com> wrote:

> On Wed, Feb 25, 2015 at 10:43 AM, Ionel Cristian Mărieș
> <contact at ionelmc.ro> wrote:
> > 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).
>
> Are you sure their hashes are computed from contained values? I just
> tried it, generated two exceptions that should be indistinguishable,
> and they had different hashes:
>
> rosuav at sikorsky:~$ python3
> Python 3.5.0a0 (default:4709290253e3, Jan 20 2015, 21:48:07)
> [GCC 4.7.2] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> try: open("/root/asdf")
> ... except OSError as e: e1=e
> ...
> >>> try: open("/root/asdf")
> ... except OSError as e: e2=e
> ...
> >>> hash(e1)
> -9223363249876220580
> >>> hash(e2)
> -9223363249876220572
> >>> type(e1).__hash__
> <slot wrapper '__hash__' of 'object' objects>
>
> I suspect the last line implies that there's no __hash__ defined
> anywhere in the exception hierarchy, and it's using the default hash
> implementation from object(). In any case, those two exceptions appear
> to have different hashes, and they have the same args and filename.
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150225/2a483e53/attachment.html>


More information about the Python-ideas mailing list