class 'Exception', unable to use 'super' to call superclass initializer

Discussion in 'Python' started by chriss, Sep 10, 2005.

  1. chriss

    chriss Guest

    Hi,

    environment: Python 2.4, GNU/Linux, kernel 2.6.12.2

    having subclassed 'Exception' I'm trying to call the initialiser
    __init__(...) of the superclass Exception with 'super(..).__init__(..)' .
    However, trying to do so results in a
    'TypeError: super() argument 1 must be type, not classobj'.

    Now, if I use 'Exception.__init__(..)' instad of super(..)... ,everything
    works just as one would expect.

    Why does 'super(..).__init__(..)' fail?


    thank you for any suggestions
    chriss



    Here is some example code to illustrate the point:


    class WorkingException(Exception):

    def __init__(self, message):
    # works as I would expect
    Exception.__init__(self, message)


    class BrokenException(Exception):

    def __init__(self, message):
    # fails with a typeError
    super(BrokenException, self).__init__(self, message)


    # --------- case 1 ---------
    try:
    raise WorkingException("Hello WorkingException")

    except WorkingException, e:
    print e


    # --------- case 3 ---------

    try:
    raise BrokenException("Hello BrokenException")


    except BrokenException, e:
    print e
     
    chriss, Sep 10, 2005
    #1
    1. Advertising

  2. chriss

    Robert Kern Guest

    Re: class 'Exception', unable to use 'super' to call superclassinitializer

    chriss wrote:
    > Hi,
    >
    > environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
    >
    > having subclassed 'Exception' I'm trying to call the initialiser
    > __init__(...) of the superclass Exception with 'super(..).__init__(..)' .
    > However, trying to do so results in a
    > 'TypeError: super() argument 1 must be type, not classobj'.
    >
    > Now, if I use 'Exception.__init__(..)' instad of super(..)... ,everything
    > works just as one would expect.
    >
    > Why does 'super(..).__init__(..)' fail?


    AFAICT, the Exception hierarchy are still old-style classes while
    super() only works on new-style classes.

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Sep 10, 2005
    #2
    1. Advertising

  3. chriss

    Peter Hansen Guest

    chriss wrote:
    > Hi,
    >
    > environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
    >
    > having subclassed 'Exception' I'm trying to call the initialiser
    > __init__(...) of the superclass Exception with 'super(..).__init__(..)' .
    > However, trying to do so results in a
    > 'TypeError: super() argument 1 must be type, not classobj'.
    >
    > Now, if I use 'Exception.__init__(..)' instad of super(..)... ,everything
    > works just as one would expect.
    >
    > Why does 'super(..).__init__(..)' fail?


    Exceptions do not inherit from 'object'; they are old-style classes.

    super() can be used only with new-style classes (which subclass 'object').

    -Peter
     
    Peter Hansen, Sep 10, 2005
    #3
  4. chriss

    chriss Guest

    Peter Hansen wrote:

    > chriss wrote:
    >> Hi,
    >>
    >> environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
    >>
    >> having subclassed 'Exception' I'm trying to call the initialiser
    >> __init__(...) of the superclass Exception with 'super(..).__init__(..)' .
    >> However, trying to do so results in a
    >> 'TypeError: super() argument 1 must be type, not classobj'.
    >>
    >> Now, if I use 'Exception.__init__(..)' instad of super(..)... ,everything
    >> works just as one would expect.
    >>
    >> Why does 'super(..).__init__(..)' fail?

    >
    > Exceptions do not inherit from 'object'; they are old-style classes.
    >
    > super() can be used only with new-style classes (which subclass 'object').
    >
    > -Peter


    That explains it all right.
    Thank you very much for your answer.

    chriss
     
    chriss, Sep 10, 2005
    #4
    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. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,821
    Darryl Pierce
    Feb 24, 2005
  2. Evan Klitzke
    Replies:
    0
    Views:
    373
    Evan Klitzke
    Aug 2, 2007
  3. Ben Finney

    super() doesn't get superclass

    Ben Finney, Sep 18, 2007, in forum: Python
    Replies:
    53
    Views:
    1,331
    OKB (not okblacke)
    Sep 20, 2007
  4. bart van deenen
    Replies:
    6
    Views:
    783
    bart van deenen
    Mar 3, 2009
  5. fi3nd
    Replies:
    0
    Views:
    388
    fi3nd
    Apr 8, 2010
Loading...

Share This Page