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
  • 103,475
  • 75
  • 157
  • 194

4 Answers4

9

super() only works for new-style classes

Patrick McElhaney
  • 55,601
  • 40
  • 126
  • 165
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
  • 6,802
  • 7
  • 37
  • 71
Stephan202
  • 57,780
  • 13
  • 124
  • 131
2

So if super() doesn't work use :

import cmd

class My_class(cmd.Cmd):
    def __init__(self):
        cmd.Cmd.__init__(self)
Jonathon Reinhart
  • 124,861
  • 31
  • 240
  • 314
themadmax
  • 2,198
  • 1
  • 28
  • 34
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
  • 407
  • 4
  • 7