Python Classes: Old-style vs New-style


I ran into a bit of a problem, trying to use the new-style class syntax while calling a class that apparently only supports the old-class style syntax (thanks due to Brend on #python at irc.freenode.net for pointing that out). Look closely at the call to super() in the following code:

#!/usr/bin/python
# -*- set tabstop=4 -*-

import cgi

class CGIWrapper(cgi.FieldStorage):
    def __init__(self):
        super(CGIWrapper, self).__init__()

if __name__ == "__main__":
    c = CGIWrapper()

---------Error----------------
Traceback (most recent call last):
 File "yoyo.py", line 17, in ?
  c = CGIWrapper()
 File "yoyo.py", line 9, in __init__
  super(CGIWrapper, self).__init__()
TypeError: super() argument 1 must be type, not classobj

The workaround, naturally, is to call cgi.FieldStorage using the old-class style way.


class CGIWrapper(cgi.FieldStorage):
    def __init__(self):
        cgi.FieldStorage.__init__(self)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s