"Super()" confusion

Robert Kern robert.kern at gmail.com
Tue Feb 10 00:27:18 CET 2009


On 2009-02-09 17:20, Lionel wrote:
> Hello. I've been scouring the web looking for something to clear up a
> little confusion about the use of "super()" but haven't found anything
> that really helps. Here's my simple example:
>
>
> class Parent:
>       def __init__(self, filePath):
>         .
>         .
>         Do some processing with "filePath"
>         .
>         .
>
> class Child(Parent):
>       def __init__(self, filePath):
>            super(Child,self).__init__(filePath)
>            .
>            .
>            Do some additional Child-specific processing on filePath
>            .
>            .
>
> As I understand it, "super()" will invoke the initializer of the base
> class. There must be something wrong with the above syntax since I'm
> getting:
>
> "super(Child,self).__init__(filePath)
> TypeError: super() argument 1 must be type, not classobj"
>
> What have I done wrong? Thanks in advance for any help.

super() only works on the "new-style" classes introduced in Python 2.2. You need 
to make Parent subclass from object:

class Parent(object):
     ...

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list