473,387 Members | 1,545 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,387 software developers and data experts.

Why does super take a class name as the argument?

Good day,

I've done a bit of searching in the language reference and a couple
pages referring the behavior of super() but I can't find any
discussion of why super needs the name of the class as an argument.
Feel free to point me into the bowels of google if this has been
discussed to death already.

super(self).method() seems like super could just do the right
thing...

super(kls).method() could also work if you needed access up the tree
to from a classmethod. Maybe I'm missing something with Metaclasses.

The reason I'm asking about this is I find myself cursing super every
time I want to change the name of the class I'm working on when one of
the reason's that super was implemented (I'm guessing) was to insulate
derived classes from the name changes of where they inherited from.

Thanks,
Chris
--
Chris Green <cm*@dok.org>
"Yeah, but you're taking the universe out of context."
Jul 18 '05 #1
10 2096
Chris Green wrote:
... I can't find any
discussion of why super needs the name of the class as an argument.

super(self).method() seems like super could just do the right
thing...

It could "just do the right thing", but not as a function, only as a
compiler magic thingamiebob. When methods are being translated, they
are ordinary function definitions, and the class for which they will
become a method does not exist. By including the name of the class
itself, rather than the name of the (or a) super class, super can search
in the "mro" for the class, and find the next class in that order after
the given class. "super" seems to me to have the "explicit is better
than implicit" flavor without forcing you to know the whole inheritance
structure.

-Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #2
Chris Green <cm*@dok.org> wrote:
I've done a bit of searching in the language reference and a couple
pages referring the behavior of super() but I can't find any
discussion of why super needs the name of the class as an argument.
http://www.python.org/2.2.1/descrintro.html#mro and
http://www.python.org/2.2.1/descrintro.html#cooperation
super(self).method() seems like super could just do the right
thing...

super(kls).method() could also work if you needed access up the tree
to from a classmethod. Maybe I'm missing something with Metaclasses.


To try and summarise (and grossly simplify), consider:

class A(object):
def method(self):
pass

class B(A):
def method(self):
super(B, self).method()

class C(A):
def method(self):
super(C, self).method()

class D(B, C):
def method(self):
super(A, self).method()

If super worked just off self, how are the method()s in B and C
supposed to know that they should be calling A.method() (since
isinstance(self, D))? If super worked just off the class, both
B.method() and C.method() would call A.method() when called
from D.method(). So super needs to know both the class of self
and the class the method it is being used from belongs to. And
if you think the latter is easy for the compiler, consider

def generic_method(self):
...

B.method = generic_method

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Jul 18 '05 #3
Chris Green wrote:
.. . .
The reason I'm asking about this is I find myself cursing super every
time I want to change the name of the class I'm working on when one of
the reason's that super was implemented (I'm guessing) was to insulate
derived classes from the name changes of where they inherited from.


Derived classes *are* insulated from name changes of the base classes.
The class in super has to be the same as the class in which the super
call is written (i.e. the one 'around' the super call), not a base
class. So if you change a class name, you only change the super calls
that exist within that class.

Of course, it could be easier with more support from the interpreter
(and it may be in a future version). See also autosuper in Guido's essay [1]

Shalabh

[1] http://www.python.org/2.2.3/descrintro.html

Jul 18 '05 #4
Shalabh Chaturvedi <sh*****@cafepy.com> writes:
Derived classes *are* insulated from name changes of the base
classes. The class in super has to be the same as the class in
which the super call is written (i.e. the one 'around' the super
call),
That's what I meant to say. Sorry if I was unclear.
[1] http://www.python.org/2.2.3/descrintro.html


Thanks for the pointer ( to all ). This was what I was looking for.
--
Chris Green <cm*@dok.org>
Laugh and the world laughs with you, snore and you sleep alone.
Jul 18 '05 #5
Chris Green wrote:
I've done a bit of searching in the language reference and a couple
pages referring the behavior of super() but I can't find any
discussion of why super needs the name of the class as an argument.


Think about it. In this code:

class A(object):
def do_stuff(self):
print "A is doing stuff now."
class B(A):
def do_stuff(self):
super(B, self).do_stuff()
print "B is doing stuff now."
class C(B):
def do_stuff(self):
super(C, self).do_stuff()
print "C is doing stuff now."
How would Python know that B should call C's do_stuff() method instead
of its own if there was no class argument? The self argument would be
exactly the same when C called super() as when B called it. There has
been some talk of making super into a language keyword instead of a
type, though; that would eliminate the need to even pass in self.
Jul 18 '05 #6
Chris Green wrote:
[1] http://www.python.org/2.2.3/descrintro.html


Thanks for the pointer ( to all ). This was what I was looking for.


Here's another:
http://www.cafepy.com/articles/pytho...hods/ch02.html

--
Shalabh

Jul 18 '05 #7
Leif K-Brooks wrote:
Chris Green wrote:
I've done a bit of searching in the language reference and a couple
pages referring the behavior of super() but I can't find any
discussion of why super needs the name of the class as an argument.

Think about it. In this code:

class A(object):
def do_stuff(self):
print "A is doing stuff now."
class B(A):
def do_stuff(self):
super(B, self).do_stuff()
print "B is doing stuff now."
class C(B):
def do_stuff(self):
super(C, self).do_stuff()
print "C is doing stuff now."
How would Python know that B should call C's do_stuff() method instead
of its own if there was no class argument? The self argument would be
exactly the same when C called super() as when B called it. There has
been some talk of making super into a language keyword instead of a
type, though; that would eliminate the need to even pass in self.


What's the going argument against it? Makes sense to me.

Jul 18 '05 #8
On Oct 15, 2004, at 7:36 PM, dataangel wrote:
What's the going argument against it? Makes sense to me.


There are constructions in other languages whereby an call to the
superclass method does not require an explicit name of the current
class. When an inherited method is augmented in a subclass, the call to
the superclass version of the method doesn't require any class
specification; each class knows its own hierarchy.

So in the examples others have given, you'd have:

class A(object):
def theMethod(self):
print "doing A stuff"

def someOtherMethod(self):
print "Some Other A method is running"

class B(A):
def theMethod(self):
print "doing some early B stuff"
self.super()
print "some final B stuff"

When B is created and its theMethod() is called, it prints as follows:
obj = B()
obj.theMethod() doing some early B stuff
doing A stuff
some final B stuff obj.someOtherMethod()

Some Other A method is running

In other words, the B class knows it inherits from the A class. When
the call to super() is encountered, B looks to its superclass for such
a method, and executes it if found. If it has multiple parents, it is
resolved as is normally done with multiple inheritance. When the call
to someOtherMethod() is made, the B object knows that it should execute
that method is its superclass.

Think of it another way: since B can figure out how to execute a call
to a method that doesn't exist in B itself by looking into its class
hierarchy, why can't a call like self.super() execute the code that
would have been executed had the subclass not overridden the method in
the first place?

___/
/
__/
/
____/
Ed Leafe
http://leafe.com/
http://dabodev.com/

Jul 18 '05 #9
In article <ma**************************************@python.o rg>,
Ed Leafe <ed@leafe.com> wrote:
On Oct 15, 2004, at 7:36 PM, dataangel wrote:
What's the going argument against it? Makes sense to me.
There are constructions in other languages whereby an call to the
superclass method does not require an explicit name of the current
class. When an inherited method is augmented in a subclass, the call to
the superclass version of the method doesn't require any class
specification; each class knows its own hierarchy.


Except that in python a method can be changed to a different class, or
even multiple classes, not to mention the fact that class parents can be
dynamically reassigned.

In lanagues like Self, which is similar in its flexibility (but can
actually handle this sort of thing), the method itself inherits from the
object (it's kind of confusing to wrap your head around) so it can
figure out things like this, but Python doesn't work that way.

Think of it another way: since B can figure out how to execute a call
to a method that doesn't exist in B itself by looking into its class
hierarchy, why can't a call like self.super() execute the code that
would have been executed had the subclass not overridden the method in
the first place?


When an instance "b" (or say "c" which is an instance of class "C" which
is a subclass of "B") executes something like "self.foo", it starts
looking at the class of self. "super" actually starts further up the
chain - if self is "c", you want "A.foo" and not "B.foo" (which is what
you'd get if it tried to derive who "super" is based on "self").
Jul 18 '05 #10
Chris Green <cm*@dok.org> wrote in message news:<87************@ion.xlipstream.com>...
Good day,

I've done a bit of searching in the language reference and a couple
pages referring the behavior of super() but I can't find any
discussion of why super needs the name of the class as an argument.
Feel free to point me into the bowels of google if this has been
discussed to death already.


Others have already answered your question. I want just to point out
that there exists recipes to avoid retyping the class name: the first
of these is Guido's autosuper recipe. A very sophysticated one is this one:

http://aspn.activestate.com/ASPN/Coo.../Recipe/286195

I also wrote one of such recipes -;)

http://aspn.activestate.com/ASPN/Coo.../Recipe/284528

Michele Simionato
Jul 18 '05 #11

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

Similar topics

3
by: Gonēalo Rodrigues | last post by:
Hi, Ok, now I'm really confused. What is supposed super(<class>, <subclass of class>) to do? My thought was that with the following setup:
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:...
0
by: Michele Simionato | last post by:
Here is an idea for a nicer syntax in cooperative method calls, which is not based on Guido's "autosuper" example. This is just a hack, waiting for a nicer "super" built-in ... Here is example...
5
by: Tobiah | last post by:
What is the purpose of the second argument to super()? What is meant by the returning of an 'unbound' object when the argument is omitted. Also, when would I pass an object as the second...
6
by: Steven Bethard | last post by:
When would you call super with only one argument? The only examples I can find of doing this are in the test suite for super. Playing around with it: py> class A(object): .... x = 'a'...
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):...
6
by: David Hirschfield | last post by:
I'm having trouble with the new descriptor-based mechanisms like super() and property() stemming, most likely, from my lack of knowledge about how they work. Here's an example that's giving me...
9
by: Mike Krell | last post by:
I'm reading Alex Martelli's "Nutshell" second edition. In the section called "Cooperative superclass method calling", he presents a diamond inheritance hierachy: class A(object): def...
3
by: metaperl | last post by:
On p.282 of "Python Cookbook" and in the Python docs on calling super: http://www.python.org/download/releases/2.2.3/descrintro/#cooperation it is clear that the first argument to super is a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.