"Super()" confusion

Discussion in 'Python' started by Lionel, Feb 9, 2009.

  1. Lionel

    Lionel Guest

    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.

    L
    Lionel, Feb 9, 2009
    #1
    1. Advertising

  2. Lionel

    Robert Kern Guest

    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
    Robert Kern, Feb 9, 2009
    #2
    1. Advertising

  3. >>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:
    >>
    >> [snip]
    >>
    >>"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.

    >
    > Consider whether you really need to use super().
    >
    > http://fuhm.net/super-harmful/


    Did you actually read that article, understood it, went through the
    tons of responses from python-dev team members, including Guido, or
    simply read its title, did absolutely no serious thinking about it and
    go on throwing it around as if it would prove anything?

    I can't believe this "super( ) is harmful" BS is still alive, it grew
    to be a mature internet meme like the dancing hamster or star wars kid
    :)

    See (among tons of other writings):

    http://mail.python.org/pipermail/python-dev/2005-January/thread.html

    Cheers,
    Daniel

    --
    Psss, psss, put it down! - http://www.cafepress.com/putitdown
    Daniel Fetchinson, Feb 10, 2009
    #3
  4. Jean-Paul Calderone <exarkun <at> divmod.com> writes:
    > Consider whether you really need to use super().
    >
    > http://fuhm.net/super-harmful/


    This article chiefly deals with super()'s harm in multiple inteheritance
    situations. For the simple case, though, like that presented by the OP, I
    believe super() is perfect.
    Benjamin Peterson, Feb 10, 2009
    #4
  5. On Tue, 10 Feb 2009 02:02:43 +0000, Benjamin Peterson wrote:

    > Jean-Paul Calderone <exarkun <at> divmod.com> writes:
    >> Consider whether you really need to use super().
    >>
    >> http://fuhm.net/super-harmful/

    >
    > This article chiefly deals with super()'s harm in multiple inteheritance
    > situations. For the simple case, though, like that presented by the OP,
    > I believe super() is perfect.


    But for the simple cases it is unnecessary because it was invented to
    deal with multiple inheritance problems.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Feb 10, 2009
    #5
  6. On Feb 10, 10:42 am, Marc 'BlackJack' Rintsch <> wrote:
    > On Tue, 10 Feb 2009 02:02:43 +0000, Benjamin Peterson wrote:
    > > Jean-Paul Calderone <exarkun <at> divmod.com> writes:
    > >> Consider whether you really need to use super().

    >
    > >>http://fuhm.net/super-harmful/

    >
    > > This article chiefly deals with super()'s harm in multiple inteheritance
    > > situations. For the simple case, though, like that presented by the OP,
    > > I believe super() is perfect.

    >
    > But for the simple cases it is unnecessary because it was invented to
    > deal with multiple inheritance problems.
    >
    > Ciao,
    >         Marc 'BlackJack' Rintsch


    Unfortunately there is no good solution. If you have a single
    inheritance hierarchy and you do
    not use super, you make it impossible for users of your hierarchy to
    subclass it by using multiple inheritance in a cooperative way (this
    is not polite).
    OTOH, if you user super, your users must know about it, to avoid
    unexpected cooperation. It is all explained in the "super harmful"
    paper, which is a must read even if you only use single inheritance.
    Michele Simionato, Feb 10, 2009
    #6
  7. On Tue, 10 Feb 2009 02:11:10 -0800, Michele Simionato wrote:

    > Unfortunately there is no good solution. If you have a single
    > inheritance hierarchy and you do not use super, you make it impossible
    > for users of your hierarchy to subclass it by using multiple
    > inheritance in a cooperative way (this is not polite).


    So I'm impolite. :) I'm using multiple inheritance only for mixin
    classes and even that quite seldom. No `super()` in my code.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Feb 10, 2009
    #7
  8. Marc 'BlackJack' Rintsch <bj_666 <at> gmx.net> writes:

    >
    > On Tue, 10 Feb 2009 02:02:43 +0000, Benjamin Peterson wrote:
    >
    > > Jean-Paul Calderone <exarkun <at> divmod.com> writes:
    > >> Consider whether you really need to use super().
    > >>
    > >> http://fuhm.net/super-harmful/

    > >
    > > This article chiefly deals with super()'s harm in multiple inteheritance
    > > situations. For the simple case, though, like that presented by the OP,
    > > I believe super() is perfect.

    >
    > But for the simple cases it is unnecessary because it was invented to
    > deal with multiple inheritance problems.


    super() is great for single inheritance because it furthers the DRY principle
    (base classes are not scattered through the code), and rather ugly use of
    unbound methods.
    Benjamin Peterson, Feb 10, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,762
    Darryl Pierce
    Feb 24, 2005
  2. Fernando Rodriguez

    Getting the super class via the super() function

    Fernando Rodriguez, Nov 21, 2003, in forum: Python
    Replies:
    2
    Views:
    722
    Bob Willan
    Nov 22, 2003
  3. Kerim Borchaev

    super. could there be a simpler super?

    Kerim Borchaev, Jan 15, 2004, in forum: Python
    Replies:
    4
    Views:
    477
    Michele Simionato
    Jan 15, 2004
  4. Replies:
    7
    Views:
    450
    Patricia Shanahan
    Apr 6, 2008
  5. GOOD LUCK GROUP
    Replies:
    0
    Views:
    395
    GOOD LUCK GROUP
    Apr 28, 2008
Loading...

Share This Page