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: Deadly Dirk on 17 Jun 2010 12:36 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? -- The missionaries go forth to Christianize the savages - as if the savages weren't dangerous enough already.
From: Deadly Dirk on 17 Jun 2010 12:53 On Thu, 17 Jun 2010 16:36:10 +0000, 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? I tried this, too: >>> class C(P): def __init__(self): super(__class__).__init__(self) print("I am a member of class C") >>> x=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 >>> -- The missionaries go forth to Christianize the savages - as if the savages weren't dangerous enough already.
From: J. Cliff Dyer on 17 Jun 2010 13:48 On Thu, 2010-06-17 at 16:36 +0000, 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? > 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. > -- > The missionaries go forth to Christianize the savages - > as if the savages weren't dangerous enough already.
From: Matteo Landi on 17 Jun 2010 13:51 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' >>> class P: .... 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: >>> class C(P): .... def __init__(self): .... super().__init__(self) .... print("I am a member of class C") Now it should work (not tested). On Thu, Jun 17, 2010 at 6:53 PM, Deadly Dirk <dirk(a)plfn.invalid> wrote: > On Thu, 17 Jun 2010 16:36:10 +0000, 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? > > I tried this, too: > >>>> class C(P): > def __init__(self): > super(__class__).__init__(self) > print("I am a member of class C") > > >>>> x=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 >>>> > > > > -- > The missionaries go forth to Christianize the savages - > as if the savages weren't dangerous enough already. > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/
From: Deadly Dirk on 17 Jun 2010 14:24
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 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 ==== >>> class P: def __init__(self): print("I am a member of class P") >>> class C(P): def __init__(self): super().__init__() print("I am a member of class C") >>> x=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 ==== >>> class P: def __init__(self): print("I am a member of class P") >>> class C(P): def __init__(self): super().__init__() print("I am a member of class C") >>> x=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) >>> -- The missionaries go forth to Christianize the savages - as if the savages weren't dangerous enough already. |