473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

exception question

Hi,

For error processing I found convenient maintaining a dictionary where
the keys are exception *classes* and the values are callables. Of
course, for this to work, exception classes have to be hashable which
I happily found that they were. So my question is, can I count on this
behaviour? Or is this behaviour I should not count on? (I found
nothing on the docs about it, thus the question).

With my best regards,
G. Rodrigues
Jul 18 '05 #1
6 1655
Gonçalo Rodrigues wrote:
Hi,

For error processing I found convenient maintaining a dictionary where
the keys are exception *classes* and the values are callables. Of
course, for this to work, exception classes have to be hashable which
I happily found that they were. So my question is, can I count on this
behaviour? Or is this behaviour I should not count on? (I found
nothing on the docs about it, thus the question).


Did you try ? I guess you didn't :

Python 2.2.2 (#2, Feb 5 2003, 10:40:08)
[GCC 3.2.1 (Mandrake Linux 9.1 3.2.1-5mdk)] on linux-i386
Type "help", "copyright", "credits" or "license" for more information.
class TestError(Exception): pass .... d = {TestError: "TestError"}
d {<class __main__.TestError at 0x80a66bc>: 'TestError'} l = [1,2,3]
d = {l : "truc"} Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list objects are unhashable


BTW, it seems that classes are hashable !-)

Bruno

Jul 18 '05 #2
Gonçalo Rodrigues wrote:
For error processing I found convenient maintaining a dictionary where
the keys are exception *classes* and the values are callables. Of
course, for this to work, exception classes have to be hashable which
I happily found that they were. So my question is, can I count on this
behaviour? Or is this behaviour I should not count on? (I found
nothing on the docs about it, thus the question).


(No answer to your question)

import sys

class MyException(Exception):
def __init__(self, msg, handler):
Exception.__init__(self, msg)
self.handler = handler

try:
raise MyException("yup", lambda: sys.stdout.write("call it sleep\n"))
except MyException, e:
e.handler()

Would that eliminate the need for a dictionary?

Peter
Jul 18 '05 #3
On Mon, 01 Sep 2003 00:14:13 +0200, Peter Otten <__*******@web.de>
wrote:
Gonçalo Rodrigues wrote:
For error processing I found convenient maintaining a dictionary where
the keys are exception *classes* and the values are callables. Of
course, for this to work, exception classes have to be hashable which
I happily found that they were. So my question is, can I count on this
behaviour? Or is this behaviour I should not count on? (I found
nothing on the docs about it, thus the question).


(No answer to your question)

import sys

class MyException(Exception):
def __init__(self, msg, handler):
Exception.__init__(self, msg)
self.handler = handler

try:
raise MyException("yup", lambda: sys.stdout.write("call it sleep\n"))
except MyException, e:
e.handler()

Would that eliminate the need for a dictionary?


Yup, I did though of of a scheme like that before -- It's my fall back
solution in case I should not count on the fact that exception classes
are hashable.

Thanks anyway,
G. Rodrigues

Jul 18 '05 #4
In article <lf********************************@4ax.com>,
Gonçalo Rodrigues <op*****@mail.telepac.pt> wrote:

For error processing I found convenient maintaining a dictionary where
the keys are exception *classes* and the values are callables. Of
course, for this to work, exception classes have to be hashable which
I happily found that they were. So my question is, can I count on this
behaviour? Or is this behaviour I should not count on? (I found
nothing on the docs about it, thus the question).


If it's not documented, you can't count on it. There's no intrinsic
reason exception classes would be made unhashable, *but* classes become
unhashable as soon as they implement __cmp__() or __eq__() *and* they
fail to implement __hash__(). So if someone ever asks to compare
exceptions, it could be an issue.

You may want to bring this issue up on python-dev.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
Jul 18 '05 #5
Aahz wrote:
In article <lf********************************@4ax.com>,
Gonçalo Rodrigues <op*****@mail.telepac.pt> wrote:

For error processing I found convenient maintaining a dictionary where
the keys are exception *classes* and the values are callables. Of
course, for this to work, exception classes have to be hashable which
I happily found that they were. So my question is, can I count on this
behaviour? Or is this behaviour I should not count on? (I found
nothing on the docs about it, thus the question).
If it's not documented, you can't count on it. There's no intrinsic
reason exception classes would be made unhashable, *but* classes become
unhashable as soon as they implement __cmp__() or __eq__() *and* they
fail to implement __hash__(). So if someone ever asks to compare
exceptions, it could be an issue.


A new-style class would NOT become unhashable by implementing __eq__
w/o __hash__, although its INSTANCES would (but Gonçalo is clearly
indicating that he cares about classes, not instances). Unfortunately
exception classes are old-style, so they'd be subject to the darned
old confusion between class and instance. HOWEVER, one might hope
that if ever standard exception classes ARE changed in a way that is
not backwards compatible, by adding __eq__ &c, then at the same time
they might be made new-style classes, which would in any case have
much lesser potential for old-code breakage.

You may want to bring this issue up on python-dev.


Good suggestion -- but I'd further suggest the class-vs-instance
and old-vs-new classes distinctions be clearly kept in mind when
so doing.
Alex

Jul 18 '05 #6
On Sun, 31 Aug 2003 22:22:15 +0100, Gonçalo Rodrigues <op*****@mail.telepac.pt> wrote:
Hi,

For error processing I found convenient maintaining a dictionary where
the keys are exception *classes* and the values are callables. Of
course, for this to work, exception classes have to be hashable which
I happily found that they were. So my question is, can I count on this
behaviour? Or is this behaviour I should not count on? (I found
nothing on the docs about it, thus the question).

Would the class name strings work for you, to avoid the issue?

Regards,
Bengt Richter
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the...
12
by: Bret Pehrson | last post by:
Suppose the following: // Unmanaged code class UnmanagedException /* not visible outside of unmanaged code */ { }; void DoSomething() /* visible (exported) to managed code */ { throw new...
10
by: tony | last post by:
Hello!! As you know every user defined exception must be derived from class Exception. Now to my question if I write catch then every exception will be caught. If I instead write...
3
by: JohnDeHope3 | last post by:
First let me say that I understand that Asp.Net wraps my exception in an HttpUnhandledException. I have found a lot of discussion about that on the web, which was informative, but not helpful. Let...
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
5
by: Vijay | last post by:
Hi All, I am not able to figure out what exactly happening in below code. what is control flow. Can anyone clear my confusion? Code: class A { public: A(){cout<<"In Constructor\n";}
2
by: Bob Altman | last post by:
Hi all, We have a native class modeled after the System::Exception class, and all exceptions that we throw derive from this class. For now this class is quite simple: just Description and...
9
by: =?Utf-8?B?UmFq?= | last post by:
How do I know which methods will throw exception when I am using FCL or other third party .Net library? I am developer of mostly native Windows applications and now .Net. After working few...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.