using super

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sashan

    using super

    I'm trying to use super but keep getting the following error

    E:\Code\Python\ Nexus\Player>py thon ./super_test.py
    Traceback (most recent call last):
    File "./super_test.py", line 14, in ?
    main()
    File "./super_test.py", line 11, in main
    b = B()
    File "./super_test.py", line 7, in __init__
    super(B, self).__init()
    TypeError: super() argument 1 must be type, not classobj

    Here's the code:
    class A:
    def __init__(self):
    print 'A'

    class B(A):
    def __init__(self):
    super(B, self).__init()
    print 'B'

    def main():
    b = B()

    if __name__ == '__main__':
    main()


  • Matt Goodall

    #2
    Re: using super

    sashan wrote:
    [color=blue]
    > I'm trying to use super but keep getting the following error
    >
    > E:\Code\Python\ Nexus\Player>py thon ./super_test.py
    > Traceback (most recent call last):
    > File "./super_test.py", line 14, in ?
    > main()
    > File "./super_test.py", line 11, in main
    > b = B()
    > File "./super_test.py", line 7, in __init__
    > super(B, self).__init()
    > TypeError: super() argument 1 must be type, not classobj
    >
    > Here's the code:
    > class A:
    > def __init__(self):
    > print 'A'
    > class B(A):
    > def __init__(self):
    > super(B, self).__init()
    > print 'B'
    > def main():
    > b = B()
    > if __name__ == '__main__':
    > main()
    >[/color]

    super() only works for "new style" classes, that is classes that extend
    Python's object type. class A(object) should solve your problem.

    Cheers, Matt

    --
    Matt Goodall, Pollenation Internet Ltd
    w: http://www.pollenation.net
    e: matt@pollenatio n.net



    Comment

    • Paul McGuire

      #3
      Re: using super

      > Here's the code:

      You will also need to correct:
      super(B, self).__init()
      to read
      super(B, self).__init__( )

      [color=blue]
      > class A:
      > def __init__(self):
      > print 'A'
      >
      > class B(A):
      > def __init__(self):
      > super(B, self).__init() <<<====
      > print 'B'
      >
      > def main():
      > b = B()
      >
      > if __name__ == '__main__':
      > main()
      >[/color]



      Comment

      Working...