[Python-ideas] Comparable exceptions
Chris Angelico
rosuav at gmail.com
Wed Feb 25 00:57:27 CET 2015
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
More information about the Python-ideas
mailing list