Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Cmd module, subclassing issue

Tags:

python

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?

like image 678
Teifion Avatar asked Apr 20 '09 21:04

Teifion


3 Answers

super() only works for new-style classes

like image 194
Patrick McElhaney Avatar answered Nov 19 '22 08:11

Patrick McElhaney


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.

like image 8
Stephan202 Avatar answered Nov 19 '22 07:11

Stephan202


So if super() doesn't work use :

import cmd

class My_class(cmd.Cmd):
    def __init__(self):
        cmd.Cmd.__init__(self)
like image 2
themadmax Avatar answered Nov 19 '22 06:11

themadmax