exception question

Discussion in 'Python' started by Gonçalo Rodrigues, Aug 31, 2003.

  1. 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
     
    Gonçalo Rodrigues, Aug 31, 2003
    #1
    1. Advertising

  2. Gonçalo Rodrigues

    Peter Otten Guest

    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
     
    Peter Otten, Aug 31, 2003
    #2
    1. Advertising

  3. 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
     
    Bruno Desthuilliers, Aug 31, 2003
    #3
  4. On Mon, 01 Sep 2003 00:14:13 +0200, Peter Otten <>
    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
     
    Gonçalo Rodrigues, Sep 1, 2003
    #4
  5. Gonçalo Rodrigues

    Aahz Guest

    In article <>,
    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).


    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 () <*> http://www.pythoncraft.com/

    This is Python. We don't care much about theory, except where it intersects
    with useful practice. --Aahz
     
    Aahz, Sep 1, 2003
    #5
  6. Aahz wrote:

    > In article <>,
    > 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).

    >
    > 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
     
    Alex Martelli, Sep 1, 2003
    #6
  7. On Sun, 31 Aug 2003 22:22:15 +0100, 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).
    >

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

    Regards,
    Bengt Richter
     
    Bengt Richter, Sep 1, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jeff
    Replies:
    0
    Views:
    1,551
  2. jeff
    Replies:
    3
    Views:
    1,719
    Scott
    Jun 26, 2003
  3. Kerri
    Replies:
    2
    Views:
    13,114
    Kevin Spencer
    Oct 27, 2003
  4. Ola
    Replies:
    0
    Views:
    561
  5. Selen
    Replies:
    0
    Views:
    2,718
    Selen
    May 28, 2004
Loading...

Share This Page