"Super()" confusion

L

Lionel

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
 
R

Robert Kern

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
 
D

Daniel Fetchinson

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
 
M

Marc 'BlackJack' Rintsch

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
 
M

Michele Simionato

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.
 
M

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).

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
 
B

Benjamin Peterson

Marc 'BlackJack' Rintsch said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top