super() woes (n00b)

D

Deadly Dirk

I cannot get right the super() function:
Python 3.1.1+ (r311:74480, Nov 2 2009, 14:49:22)
[GCC 4.4.1] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ==== def __init__(__class__,self):
print("I am a member of class P")

def __init__(self):
super().__init__(self)
print("I am a member of class C")



class P:
def __init__(self):
print("I am a member of class P")

class C(P):
def __init__(self):
super().__init__(self)
print("I am a member of class C")

x=C()

That is more or less the text from the "Quick Python Book". What am I
doing wrong?
 
D

Deadly Dirk

I cannot get right the super() function: Python 3.1.1+ (r311:74480, Nov
2 2009, 14:49:22) [GCC 4.4.1] on linux2
Type "copyright", "credits" or "license()" for more information. ==== No
Subprocess ====def __init__(__class__,self):
print("I am a member of class P")

def __init__(self):
super().__init__(self)
print("I am a member of class C")



class P:
def __init__(self):
print("I am a member of class P")

class C(P):
def __init__(self):
super().__init__(self)
print("I am a member of class C")

x=C()

That is more or less the text from the "Quick Python Book". What am I
doing wrong?

I tried this, too:
def __init__(self):
super(__class__).__init__(self)
print("I am a member of class C")

Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
x=C()
File "<pyshell#29>", line 3, in __init__
super(__class__).__init__(self)
TypeError: must be type, not C
 
J

J. Cliff Dyer

I cannot get right the super() function:
Python 3.1.1+ (r311:74480, Nov 2 2009, 14:49:22)
[GCC 4.4.1] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====def __init__(__class__,self):
print("I am a member of class P")

def __init__(self):
super().__init__(self)
print("I am a member of class C")



class P:
def __init__(self):
print("I am a member of class P")

class C(P):
def __init__(self):
super().__init__(self)
print("I am a member of class C")

x=C()

That is more or less the text from the "Quick Python Book". What am I
doing wrong?

super gives you an instantiated version of the super class, which means
that you don't have to explicitly send self to any methods you call on
it.

So use `super().__init__()` instead.
 
M

Matteo Landi

I found few error in your code:
1 the constructor of P class seems to be wrong:
.... def __init__(self):
.... print("I am a member of class P")
....

2 super() works with new style classes, i.e. the ones which inherit
from 'object'
.... def __init__(__class__,self):
.... print("I am a member of class P")
....

3 super() need a type as first argument, and an instance as second one:
.... def __init__(self):
.... super().__init__(self)
.... print("I am a member of class C")

Now it should work (not tested).

I cannot get right the super() function: Python 3.1.1+ (r311:74480, Nov
2 2009, 14:49:22) [GCC 4.4.1] on linux2
Type "copyright", "credits" or "license()" for more information. ==== No
Subprocess ====
    def __init__(__class__,self):
        print("I am a member of class P")

class C(P):
    def __init__(self):
        super().__init__(self)
        print("I am a member of class C")



class P:
    def __init__(self):
        print("I am a member of class P")

class C(P):
    def __init__(self):
        super().__init__(self)
        print("I am a member of class C")

x=C()

That is more or less the text from the "Quick Python Book". What am I
doing wrong?

I tried this, too:
   def __init__(self):
       super(__class__).__init__(self)
       print("I am a member of class C")

Traceback (most recent call last):
 File "<pyshell#30>", line 1, in <module>
   x=C()
 File "<pyshell#29>", line 3, in __init__
   super(__class__).__init__(self)
TypeError: must be type, not C
 
D

Deadly Dirk

super gives you an instantiated version of the super class, which means
that you don't have to explicitly send self to any methods you call on
it.

So use `super().__init__()` instead.

Thanks. Interestingly enough, it works in Python 3, which is what the
book is about. It doesn't work in Python 2.6

3.1:

Python 3.1.1+ (r311:74480, Nov 2 2009, 14:49:22)
[GCC 4.4.1] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ==== def __init__(self):
print("I am a member of class P")
def __init__(self):
super().__init__()
print("I am a member of class C")

I am a member of class P
I am a member of class C
2.6:
Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15)
[GCC 4.4.1] on linux2
Type "copyright", "credits" or "license()" for more information.

****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************

IDLE 2.6.4 ==== No Subprocess ==== def __init__(self):
print("I am a member of class P")
def __init__(self):
super().__init__()
print("I am a member of class C")
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
x=C()
File "<pyshell#1>", line 3, in __init__
super().__init__()
TypeError: super() takes at least 1 argument (0 given)
 
B

Benjamin Kaplan

I found few error in your code:
1 the constructor of P class seems to be wrong:

...    def __init__(self):
...        print("I am a member of class P")
...

2 super() works with new style classes, i.e.  the ones which inherit
from 'object'

The OP is using Python 3.1. All classes in Python 3.x are new style.
 
E

Ethan Furman

Deadly said:
Thanks. Interestingly enough, it works in Python 3, which is what the
book is about. It doesn't work in Python 2.6

as Thomas Jollans said days ago:
> but you should really install Python 3.1 (it's in ubuntu, as others
> have said!) because you will almost certainly hit into other snags.

or, as Gabriele Lanaro said in that same thread:
> else take a book that covers python 2.x syntax

Cut-and-pasting-ly yours,

~Ethan~
 
J

Jean-Michel Pichavant

Deadly said:
I cannot get right the super() function:
Python 3.1.1+ (r311:74480, Nov 2 2009, 14:49:22)
[GCC 4.4.1] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
def __init__(__class__,self):
print("I am a member of class P")


def __init__(self):
super().__init__(self)
print("I am a member of class C")



class P:
def __init__(self):
print("I am a member of class P")

class C(P):
def __init__(self):
super().__init__(self)
print("I am a member of class C")

x=C()

That is more or less the text from the "Quick Python Book". What am I
doing wrong?
If you're quite new to Python I would advise to drop super and use an
explicit call, sounds lame but my guess is that many people do that,
'cause explicit >> implicit. Super is meant to solve some issues about
multi inheritance, especially diamond diagram inheritance. It has no
benefit for single inheritance.

I'm pretty sure someone will state that understanding super is pretty
much easy once you've read the documenation but anticipating all the
underlying concepts may be tricky. The only situation where super is
absolutely required is when the inheritance diagram is built dynamically
during execution.
Otherwise, I would say "Have the nuts to explicit which base class
method you want to call" (easy for single inheritance though :) )

class C(P):
def __init__(self):
P.__init__(self)


JM
 
L

Lie Ryan

Deadly said:
I cannot get right the super() function:
Python 3.1.1+ (r311:74480, Nov 2 2009, 14:49:22) [GCC 4.4.1] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
def __init__(__class__,self):
print("I am a member of class P")

class C(P):
def __init__(self):
super().__init__(self)
print("I am a member of class C")


class P:
def __init__(self):
print("I am a member of class P")

class C(P):
def __init__(self):
super().__init__(self)
print("I am a member of class C")

x=C()

That is more or less the text from the "Quick Python Book". What am I
doing wrong?
If you're quite new to Python I would advise to drop super and use an
explicit call, sounds lame but my guess is that many people do that,
'cause explicit >> implicit. Super is meant to solve some issues about
multi inheritance, especially diamond diagram inheritance. It has no
benefit for single inheritance.

Actually there is. If you need to anticipate the possibility of someone
else (or you in the future) multiply-inheriting from your
singly-inheriting class, then your singly-inheriting class would need to
use super as well, otherwise the multiply-inheriting class might not be
properly initialized.
 
D

Deadly Dirk

as Thomas Jollans said days ago:

or, as Gabriele Lanaro said in that same thread:

Cut-and-pasting-ly yours,

~Ethan~

As you can see, I followed the advice and installed the latest Python.
 
D

Deadly Dirk

Deadly said:
I cannot get right the super() function: Python 3.1.1+ (r311:74480, Nov
2 2009, 14:49:22) [GCC 4.4.1] on linux2
Type "copyright", "credits" or "license()" for more information. ====
No Subprocess ====
def __init__(__class__,self):
print("I am a member of class P")


class C(P):
def __init__(self):
super().__init__(self)
print("I am a member of class C")



class P:
def __init__(self):
print("I am a member of class P")

class C(P):
def __init__(self):
super().__init__(self)
print("I am a member of class C")

x=C()

That is more or less the text from the "Quick Python Book". What am I
doing wrong?
If you're quite new to Python I would advise to drop super and use an
explicit call, sounds lame but my guess is that many people do that,
'cause explicit >> implicit. Super is meant to solve some issues about
multi inheritance, especially diamond diagram inheritance. It has no
benefit for single inheritance.

I'm pretty sure someone will state that understanding super is pretty
much easy once you've read the documenation but anticipating all the
underlying concepts may be tricky. The only situation where super is
absolutely required is when the inheritance diagram is built dynamically
during execution.
Otherwise, I would say "Have the nuts to explicit which base class
method you want to call" (easy for single inheritance though :) )

class C(P):
def __init__(self):
P.__init__(self)


JM

Jean-Michel, thanks for your advice. I do think that I understand the
"super" function, I used to do some C++ programming and am quite adept at
programming. I am learning Python and, as a stickler for details, I am
testing and running every little piece of code.
 
J

Jean-Michel Pichavant

Deadly said:
Deadly said:
I cannot get right the super() function: Python 3.1.1+ (r311:74480, Nov
2 2009, 14:49:22) [GCC 4.4.1] on linux2
Type "copyright", "credits" or "license()" for more information. ====
No Subprocess ====


class P:


def __init__(__class__,self):
print("I am a member of class P")




class C(P):


def __init__(self):
super().__init__(self)
print("I am a member of class C")



class P:
def __init__(self):
print("I am a member of class P")

class C(P):
def __init__(self):
super().__init__(self)
print("I am a member of class C")

x=C()

That is more or less the text from the "Quick Python Book". What am I
doing wrong?
If you're quite new to Python I would advise to drop super and use an
explicit call, sounds lame but my guess is that many people do that,
'cause explicit >> implicit. Super is meant to solve some issues about
multi inheritance, especially diamond diagram inheritance. It has no
benefit for single inheritance.

I'm pretty sure someone will state that understanding super is pretty
much easy once you've read the documenation but anticipating all the
underlying concepts may be tricky. The only situation where super is
absolutely required is when the inheritance diagram is built dynamically
during execution.
Otherwise, I would say "Have the nuts to explicit which base class
method you want to call" (easy for single inheritance though :) )

class C(P):
def __init__(self):
P.__init__(self)


JM

Jean-Michel, thanks for your advice. I do think that I understand the
"super" function, I used to do some C++ programming and am quite adept at
programming. I am learning Python and, as a stickler for details, I am
testing and running every little piece of code.
Quote from a c++ forum "There is no way (IMO) to generally refer to the
superclass in C++ because of multiple inheritance".
Python super funtion is different from the Java's one, (there's no
multiple inheritance in Java if I'm not wrong).

Think about this :

A
/ \
B C
\ /
D

python 2.5 code:

class A(object):
def foo(self):
print 'I am A'

class B(A):
def foo(self):
super(B, self).foo()
print 'I am B'

class C(A):
def foo(self):
super(C, self).foo()
print 'I am C'

class D(B,C):
def foo(self):
super(D, self).foo()
print 'I am D'

d = D()
d.foo()

What would you expect as a result ? Diffcult to say at first glance.

JM

PS : answer is
I am A
I am C
I am B
I am D

As you can see, super() is *not* the superclass otherwise 'I am A'
should have appeared twice (superclass method of B and C).
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top