473,225 Members | 1,350 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

super, apply, or __init__ when subclassing?

This is something that keeps confusing me. If you read examples of
code on the web, you keep on seeing these three calls (super, apply
and __init__) to reference the super-class. This looks to me as it is
somehow personal preference. But this would conflict with the "There
one way to do it" mind-set.

So, knowing that in python there is one thing to do something, these
three different calls must *do* domething different. But what exactly
*is* the difference?

------------ Exampel 1: -----------------------------

class B(A):
def __init__(self, *args):
A.__init__(self, args)

------------ Exampel 2: -----------------------------

class B(A):
def __init__(self, *args):
apply( A.__init__, (self,) + args)

------------ Exampel 3: -----------------------------

class B(A):
def __init__(self, *args):
super(A,self).__init__(*args)

Sep 18 '07 #1
6 1830
"exhuma.twn" <ex****@gmail.comwrites:
This is something that keeps confusing me. If you read examples of
code on the web, you keep on seeing these three calls (super, apply
and __init__) to reference the super-class. This looks to me as it is
somehow personal preference. But this would conflict with the "There
one way to do it" mind-set.

So, knowing that in python there is one thing to do something, these
three different calls must *do* domething different. But what exactly
*is* the difference?

------------ Exampel 1: -----------------------------

class B(A):
def __init__(self, *args):
A.__init__(self, args)

------------ Exampel 2: -----------------------------

class B(A):
def __init__(self, *args):
apply( A.__init__, (self,) + args)

------------ Exampel 3: -----------------------------

class B(A):
def __init__(self, *args):
super(A,self).__init__(*args)
Note that your examples 1 and 3 aren't different *calls*. They are
different ways of *getting at* the same class: either name it
explicitly (as in example 1) or use 'super' to get it for you.

Also, example 3 should instead call 'super(B, self).__init__'. If
you're going to go to the bother of actually *specifying* the class
'A', it's silly to call 'super' to get at it *again*.
The broader picture: I think you're right that this is ridiculously
difficult in Python. The community elders would have us use 'super' to
get at our inherited '__init__', but that doesn't work very well
<URL:http://fuhm.org/super-harmful/so most people use your example
1.

--
\ "The most merciful thing in the world... is the inability of |
`\ the human mind to correlate all its contents." -- Howard |
_o__) Philips Lovecraft |
Ben Finney
Sep 18 '07 #2
"exhuma.twn" <ex****@gmail.comwrote:
So, knowing that in python there is one thing to do something, these
three different calls must *do* domething different. But what exactly
*is* the difference?

------------ Exampel 1: -----------------------------

class B(A):
def __init__(self, *args):
A.__init__(self, args)

------------ Exampel 2: -----------------------------

class B(A):
def __init__(self, *args):
apply( A.__init__, (self,) + args)

------------ Exampel 3: -----------------------------

class B(A):
def __init__(self, *args):
super(A,self).__init__(*args)
Yes, they are all different.

The first one calls B's immediate base class but packs all of the
arguments together into a single tuple. Probably not what you meant.

The second one passes B's positional arguments to its immediate base
class without messing them up but uses a deprecated function to do it.
You should use "A.__init__(self, *args)" instead unless you are
concerned about multiple inheritance.

The third one skips over the __init__ method in the immediate base class
and calls the __init__ method in whatever class follows A in the MRO
instead. Probably not what you meant either.
Sep 18 '07 #3
exhuma.twn a écrit :
This is something that keeps confusing me. If you read examples of
code on the web, you keep on seeing these three calls (super, apply
and __init__) to reference the super-class. This looks to me as it is
somehow personal preference. But this would conflict with the "There
one way to do it" mind-set.
apply is deprecated. Chances are that code using it is somewhat old.
super came with the new object model in Python 2.2.1 (IIRC), and is only
useful for some corner cases involving multiple inheritence . Directly
calling the superclass's method (__init__ or whatever) is the canonical
way in the most common cases.

And BTW, the sentence is "there *should* be one - and *preferably* only
one - *obvious* way to do it" (emphasis is mine). In this case, there's
_at least_ one way do to do it, and only one (direct call) is really
obvious IMHO !-)
So, knowing that in python there is one thing to do something, these
three different calls must *do* domething different.
Indeed. But mostly because you managed to get 2 examples wrong !-)
But what exactly
*is* the difference?

------------ Exampel 1: -----------------------------

class B(A):
def __init__(self, *args):
A.__init__(self, args)
You want:

class B(A):
def __init__(self, *args):
A.__init__(self, *args)

------------ Exampel 2: -----------------------------

class B(A):
def __init__(self, *args):
apply( A.__init__, (self,) + args)
is the same as the previous, using the deprecated apply function.
------------ Exampel 3: -----------------------------

class B(A):
def __init__(self, *args):
super(A,self).__init__(*args)
You want:

class B(A):
def __init__(self, *args):
super(B,self).__init__(*args)

Sep 18 '07 #4
On Sep 18, 2:45 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
exhuma.twn a écrit :
This is something that keeps confusing me. If you read examples of
code on the web, you keep on seeing these three calls (super, apply
and __init__) to reference the super-class. This looks to me as it is
somehow personal preference. But this would conflict with the "There
one way to do it" mind-set.

apply is deprecated. Chances are that code using it is somewhat old.
super came with the new object model in Python 2.2.1 (IIRC), and is only
useful for some corner cases involving multiple inheritence . Directly
calling the superclass's method (__init__ or whatever) is the canonical
way in the most common cases.

And BTW, the sentence is "there *should* be one - and *preferably* only
one - *obvious* way to do it" (emphasis is mine). In this case, there's
_at least_ one way do to do it, and only one (direct call) is really
obvious IMHO !-)
So, knowing that in python there is one thing to do something, these
three different calls must *do* domething different.

Indeed. But mostly because you managed to get 2 examples wrong !-)
But what exactly
*is* the difference?
------------ Exampel 1: -----------------------------
class B(A):
def __init__(self, *args):
A.__init__(self, args)

You want:

class B(A):
def __init__(self, *args):
A.__init__(self, *args)
Ah.. this was a typo in my original post. Oops ;)
>
------------ Exampel 2: -----------------------------
class B(A):
def __init__(self, *args):
apply( A.__init__, (self,) + args)

is the same as the previous, using the deprecated apply function.
------------ Exampel 3: -----------------------------
class B(A):
def __init__(self, *args):
super(A,self).__init__(*args)

You want:

class B(A):
def __init__(self, *args):
super(B,self).__init__(*args)
Hmmm... and suddenly it all makes sense! Great!

Thanks all for clarifying this to a Java-Convert ;)

Sep 18 '07 #5
En Tue, 18 Sep 2007 04:33:11 -0300, exhuma.twn <ex****@gmail.comescribi�:
This is something that keeps confusing me. If you read examples of
code on the web, you keep on seeing these three calls (super, apply
and __init__) to reference the super-class. This looks to me as it is
somehow personal preference. But this would conflict with the "There
one way to do it" mind-set.

So, knowing that in python there is one thing to do something, these
three different calls must *do* domething different. But what exactly
*is* the difference?
There are a few typos in your examples. If you write them this way:
------------ Exampel 1: -----------------------------

class B(A):
def __init__(self, *args):
A.__init__(self, *args)

------------ Exampel 2: -----------------------------

class B(A):
def __init__(self, *args):
apply( A.__init__, (self,) + args)

------------ Exampel 3: -----------------------------

class B(A):
def __init__(self, *args):
super(B,self).__init__(*args)
then 2 is exactly the same as 1 but using a deprecated function. And 3 is
the same as 1 only when there is single inheritance involved (and you are
using new-style classes). But see the thread "super() doesn't get
superclass"

--
Gabriel Genellina

Sep 18 '07 #6
On Sep 18, 2:50 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Tue, 18 Sep 2007 04:33:11 -0300, exhuma.twn <exh...@gmail.comescribi?:
This is something that keeps confusing me. If you read examples of
code on the web, you keep on seeing these three calls (super, apply
and __init__) to reference the super-class. This looks to me as it is
somehow personal preference. But this would conflict with the "There
one way to do it" mind-set.
So, knowing that in python there is one thing to do something, these
three different calls must *do* domething different. But what exactly
*is* the difference?

There are a few typos in your examples. If you write them this way:
Example 3 was not really a typo on my side. While I was browsing
around for python, I saw this in a code-fragment. So I thought this
was the way to do it. Even though I thought "super(A..." does not make
sense in a "semantic" way. Instead of just accepting this as a fact I
probably should have trusted my gut-feeling and investigate.

Nonetheless the posts (and explanations) here were really helpful for
me to understand what's happening under the hood. But seeing these
three variants of doing (nearly) the same was an itch I finally
decided to scratch. So far I blindly used "Example 1" as it seemed to
work for me.
>
------------ Exampel 1: -----------------------------
class B(A):
def __init__(self, *args):
A.__init__(self, *args)
------------ Exampel 2: -----------------------------
class B(A):
def __init__(self, *args):
apply( A.__init__, (self,) + args)
------------ Exampel 3: -----------------------------
class B(A):
def __init__(self, *args):
super(B,self).__init__(*args)

then 2 is exactly the same as 1 but using a deprecated function. And 3 is
the same as 1 only when there is single inheritance involved (and you are
using new-style classes). But see the thread "super() doesn't get
superclass"

--
Gabriel Genellina

Sep 18 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kerim Borchaev | last post by:
Hello! Always when I use "super" I create a code duplication because class used as first arg to "super" is always the class where the method containing "super" was defined in: ''' class C:...
11
by: Nicolas Lehuen | last post by:
Hi, I hope this is not a FAQ, but I have trouble understanding the behaviour of the super() built-in function. I've read the excellent book 'Python in a Nutshell' which explains this built-in...
0
by: Delaney, Timothy C (Timothy) | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286195 This is a new version of super that automatically determines which method needs to be called based on the existing stack frames....
5
by: Christopher J. Bottaro | last post by:
Why don't this code work? import PRI class Poscdnld_PYIO(PRI.BasicBatch): def __init__(self, *argv): super(Poscdnld_PYIO, self).__init__(*argv) x = Poscdnld_PYIO()
2
by: Michael P. Soulier | last post by:
Ok, this works in Python on Windows, but here on Linux, with Python 2.4.1, I'm getting an error. The docs say: A typical use for calling a cooperative superclass method is: class C(B):...
7
by: Kent Johnson | last post by:
Are there any best practice guidelines for when to use super(Class, self).__init__() vs Base.__init__(self) to call a base class __init__()? The super() method only works correctly in multiple...
4
by: Noah | last post by:
Am I the only one that finds the super function to be confusing? I have a base class that inherits from object. In other words new style class: class foo (object): def __init__ (self, arg_A,...
10
by: Finger.Octopus | last post by:
Hello, I have been trying to call the super constructor from my derived class but its not working as expected. See the code: class HTMLMain: def __init__(self): self.text = "<HTML><BODY>";...
5
by: Nagarajan | last post by:
Hi group, I am confused with "super" usage..It seems to be complicated and less obvious. Here is what I need to achieve.. class A : def __init__( self ): self.x = 0 class B ( A ):
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.