Prev: Efficiency/style issues of import <module> vs. from <module>import <name>, ...
Next: Efficiency/style issues of import <module> vs. from <module> import <name>, ...
From: Benjamin Kaplan on 17 Jun 2010 14:26 On Thu, Jun 17, 2010 at 10:51 AM, Matteo Landi <landimatte(a)gmail.com> wrote: > I found few error in your code: > 1 the constructor of P class seems to be wrong: > >>>> class P(object): > ... 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.
From: Ethan Furman on 17 Jun 2010 15:18 Deadly Dirk wrote: > On Thu, 17 Jun 2010 13:48:45 -0400, J. Cliff Dyer wrote: > >> 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 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~
From: Jean-Michel Pichavant on 18 Jun 2010 05:19 Deadly Dirk wrote: > 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
From: Lie Ryan on 18 Jun 2010 06:14 On 06/18/10 19:19, Jean-Michel Pichavant wrote: > Deadly Dirk wrote: >> 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. 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. > 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)
From: Deadly Dirk on 18 Jun 2010 09:02
On Thu, 17 Jun 2010 12:18:33 -0700, Ethan Furman wrote: > Deadly Dirk wrote: >> On Thu, 17 Jun 2010 13:48:45 -0400, J. Cliff Dyer wrote: >> >>> 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 > > 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~ As you can see, I followed the advice and installed the latest Python. -- I don't think, therefore I am not. |