tags:

views:

264

answers:

2

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?

+4  A: 

super() only works for new-style classes

Patrick McElhaney
Ahhh, that'd explain a lot.
Teifion
+3  A: 

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

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

Stephan202
Is that because the cmd module is rewritten in Python 3?
Teifion
Nope. It's because in Python 3.0 all classes are "new style classes".
Stephan202
(In fact, a diff between 2.5's and 3.0's cmd.py will show you that very few changes were made between those versions.)
Stephan202
I wonder, could you change 'class Cmd:' in cmd.py to 'class Cmd(object)' and expect no problems?
John Fouhy
It would probably work. But you'd have the only python 2.x installation on earth with a new-style Cmd class... Don't let any code you ship depend on that :)
Stephan202