7

I'm trying to work out what's not working in this code:

#!/usr/bin/python

import cmd

class My_class (cmd.Cmd):
    """docstring for Twitter_handler"""
    def __init__(self):
        super(My_class, self).__init__()

if __name__ == '__main__':
    my_handler = My_class()

Here's the error I get

Traceback (most recent call last):
  File "main.py", line 12, in <module>
    my_handler = My_class()
  File "main.py", line 9, in __init__
    super(My_class, self).__init__()
TypeError: super() argument 1 must be type, not classobj

If I change the superclass of "My_class" to an object it works fine. Where am I going wrong?

Teifion
  • 108,121
  • 75
  • 161
  • 195

4 Answers4

9

super() only works for new-style classes

Patrick McElhaney
  • 57,901
  • 40
  • 134
  • 167
8

cmd.Cmd is not a new style class in Python 2.5, 2.6, 2.7.

Note that your code does not raise an exception in Python 3.0.

Dave
  • 7,555
  • 8
  • 46
  • 88
Stephan202
  • 59,965
  • 13
  • 127
  • 133
2

So if super() doesn't work use :

import cmd

class My_class(cmd.Cmd):
    def __init__(self):
        cmd.Cmd.__init__(self)
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
themadmax
  • 2,344
  • 1
  • 31
  • 36
2

You can still use super() if your MyClass extends object. This works even though the cmd.Cmd module is not a new-style class. Like this:

#!/usr/bin/python

import cmd

class My_class (cmd.Cmd, object):
    """docstring for Twitter_handler"""
    def __init__(self):
        super(My_class, self).__init__()

if __name__ == '__main__':
    my_handler = My_class()
deargle
  • 487
  • 4
  • 8