exception question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gonçalo Rodrigues

    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
  • Bruno Desthuilliers

    #2
    Re: exception question

    Gonçalo Rodrigues wrote:[color=blue]
    > 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).[/color]

    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.[color=blue][color=green][color=darkred]
    >>> class TestError(Excep tion): pass[/color][/color][/color]
    ....[color=blue][color=green][color=darkred]
    >>> d = {TestError: "TestError" }
    >>> d[/color][/color][/color]
    {<class __main__.TestEr ror at 0x80a66bc>: 'TestError'}[color=blue][color=green][color=darkred]
    >>> l = [1,2,3]
    >>> d = {l : "truc"}[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: list objects are unhashable[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    BTW, it seems that classes are hashable !-)

    Bruno

    Comment

    • Peter Otten

      #3
      Re: exception question

      Gonçalo Rodrigues wrote:
      [color=blue]
      > 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).[/color]

      (No answer to your question)

      import sys

      class MyException(Exc eption):
      def __init__(self, msg, handler):
      Exception.__ini t__(self, msg)
      self.handler = handler

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

      Would that eliminate the need for a dictionary?

      Peter

      Comment

      • Gonçalo Rodrigues

        #4
        Re: exception question

        On Mon, 01 Sep 2003 00:14:13 +0200, Peter Otten <__peter__@web. de>
        wrote:
        [color=blue]
        >Gonçalo Rodrigues wrote:
        >[color=green]
        >> 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).[/color]
        >
        >(No answer to your question)
        >
        >import sys
        >
        >class MyException(Exc eption):
        > def __init__(self, msg, handler):
        > Exception.__ini t__(self, msg)
        > self.handler = handler
        >
        >try:
        > raise MyException("yu p", lambda: sys.stdout.writ e("call it sleep\n"))
        >except MyException, e:
        > e.handler()
        >
        >Would that eliminate the need for a dictionary?[/color]

        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

        Comment

        • Aahz

          #5
          Re: exception question

          In article <lfp4lv8krqakee a3d0k0tcm86mb2m nf2v3@4ax.com>,
          Gonçalo Rodrigues <op73418@mail.t elepac.pt> wrote:[color=blue]
          >
          >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).[/color]

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

          This is Python. We don't care much about theory, except where it intersects
          with useful practice. --Aahz

          Comment

          • Alex Martelli

            #6
            Re: exception question

            Aahz wrote:
            [color=blue]
            > In article <lfp4lv8krqakee a3d0k0tcm86mb2m nf2v3@4ax.com>,
            > Gonçalo Rodrigues <op73418@mail.t elepac.pt> wrote:[color=green]
            >>
            >>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).[/color]
            >
            > 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.[/color]

            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.

            [color=blue]
            > You may want to bring this issue up on python-dev.[/color]

            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

            Comment

            • Bengt Richter

              #7
              Re: exception question

              On Sun, 31 Aug 2003 22:22:15 +0100, Gonçalo Rodrigues <op73418@mail.t elepac.pt> wrote:
              [color=blue]
              >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).
              >[/color]
              Would the class name strings work for you, to avoid the issue?

              Regards,
              Bengt Richter

              Comment

              Working...