473,770 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Second argument to super().

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 argument,
and when would I pass a type?

Thanks,

Tobiah
Jul 18 '05 #1
5 2363
Tobiah wrote:
What is the purpose of the second argument to super()?
You probably want to check the docs[1] again. They give an example:

class C(B):
def meth(self, arg):
super(C, self).meth(arg)
What is meant by the returning of an 'unbound' object
when the argument is omitted.
If you supply a second argument (an instance of the type) to super, the
returned object will be bound to that instance. For example:

py> class C(object):
.... def g(self):
.... print 'g'
....
py> class D(C):
.... def h(self):
.... print 'h'
....
py> d = D()
py> s = super(D, d)
py> d2 = D()
py> s2 = super(D, d2)
py> s == s2
False

Note that the object returned depends on the instance passed in. An
'unbound' object would not be bound in this way to the instance.

Note also that the super object returned has access only to the methods
of the superclass:

py> s.g
<bound method D.g of <__main__.D object at 0x01186130>>
py> d.g
<bound method D.g of <__main__.D object at 0x01186130>>
py> s.h
Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
AttributeError: 'super' object has no attribute 'h'
py> d.h
<bound method D.h of <__main__.D object at 0x01186130>>

Also, when would I pass an object as the second argument,
and when would I pass a type?


For most use cases, you'll probably only want an object (an instance).
You might run into the type case if you define the staticmethod __new__
in a class:

py> class S(str):
.... def __new__(cls, s):
.... return super(S, cls).__new__(cl s, 'S(%s)' %s)
....
py> S('abc')
'S(abc)'

In this case __new__ takes as a first argument the class (type) of the
object, so if you want to invoke the superclass __new__, you need to
pass the type to super. (You don't have an instance to pass.)
STeVe

[1] http://docs.python.org/lib/built-in-funcs.html#l2h-70
Jul 18 '05 #2
"Tobiah" <to**@rcsreg.co m> wrote in message
news:1110396167 .53bd67fed97c9e 826a92b284283b1 455@teranews...
What is the purpose of the second argument to super()?
I've always found the docs to be fairly confusing.
They didn't give me enough context to tell what
was going on. I also find the terminology confusing:
"type" seems to mean "new style class object", and
"object" seems to mean "instance."

What happens with the second operand is a bit of
sleight of hand. The object returned from super()
gives you access to the methods on the next level up the
mro, however when you use it to invoke a method,
then the 'self' passed to that method is the second
object, not the instance returned from super().

In most cases, this is exactly what you want, since if
the superclass method makes any changes to the
instance, you want to be able to see them after the
call completes.
What is meant by the returning of an 'unbound' object
when the argument is omitted.
This is basically for calling static methods. Since a static
method is not passed an instance, you need a version of
the object returned from super() that doesn't bind the
method to an instance.

There is also the possibility that you might really want
to call an instance or class method as an unbound method,
explicitly passing it the instance. This is the reason that
the object returned from super() can't make the distinction
automatically by simply checking for a static method.
Also, when would I pass an object as the second argument,
and when would I pass a type?
You need to pass the class object when you're calling
a class method. While __new__ is technically a static
method, for most practical purposes you can regard
it as a class method.

John Roth
Thanks,

Tobiah


Jul 18 '05 #3
John Roth wrote:
"Tobiah" <to**@rcsreg.co m> wrote in message
news:1110396167 .53bd67fed97c9e 826a92b284283b1 455@teranews...
What is the purpose of the second argument to super()?

I've always found the docs to be fairly confusing.
They didn't give me enough context to tell what
was going on. I also find the terminology confusing:
"type" seems to mean "new style class object", and
"object" seems to mean "instance."

I agree that the docs could probably do with some improvement here, but
this is mostly because (I suspect) the type-based material has been
shoehorned in to the existing documentation structure. My own suspicion
was that a more radical revision would yield a better manual, but it
doesn't look as though anybody has had time to attempt it.

Certainly super() is some of the deepest magic related to the new-style
object hierarchy.
What happens with the second operand is a bit of
sleight of hand. The object returned from super()
gives you access to the methods on the next level up the
mro, however when you use it to invoke a method,
then the 'self' passed to that method is the second
object, not the instance returned from super().
This is a key point. I wonder whether we might use this thread to draft
a patch to the docs for submission on SourceForge?
In most cases, this is exactly what you want, since if
the superclass method makes any changes to the
instance, you want to be able to see them after the
call completes.

Quite. It's important that super() doesn't create a completely new
object, because it's really about identifying an appropriate point in
the inheritance hierarchy (method resolution order) and offering
namespace access from that point up, rather than from the base instance
given as the second argument to super(). This means that the same code
can be included in many different namespace hierarchies and still work
correctly in them all.
What is meant by the returning of an 'unbound' object
when the argument is omitted.

This is basically for calling static methods. Since a static
method is not passed an instance, you need a version of
the object returned from super() that doesn't bind the
method to an instance.

There is also the possibility that you might really want
to call an instance or class method as an unbound method,
explicitly passing it the instance. This is the reason that
the object returned from super() can't make the distinction
automatically by simply checking for a static method.
Also, when would I pass an object as the second argument,
and when would I pass a type?

You need to pass the class object when you're calling
a class method. While __new__ is technically a static
method, for most practical purposes you can regard
it as a class method.

This is all good stuff. We should try to make sure the documentation
gets enhanced.

regards
Steve

Jul 18 '05 #4
Steve Holden wrote:
John Roth wrote:
"Tobiah" <to**@rcsreg.co m> wrote in message
news:1110396167 .53bd67fed97c9e 826a92b284283b1 455@teranews...
What is the purpose of the second argument to super()?
I've always found the docs to be fairly confusing.
They didn't give me enough context to tell what
was going on. I also find the terminology confusing:
"type" seems to mean "new style class object", and
"object" seems to mean "instance."

These are certainly basic terms. It could help to a glossary setting
out just what these terms mean.

In some cases type and class are used synonomously. Clarification would
help us all.
I agree that the docs could probably do with some improvement here, but
this is mostly because (I suspect) the type-based material has been
shoehorned in to the existing documentation structure. My own suspicion
was that a more radical revision would yield a better manual, but it
doesn't look as though anybody has had time to attempt it.

A radical revision here would certainly help.

Colin W.
Jul 18 '05 #5
Steve Holden wrote:
John Roth wrote:
What happens with the second operand is a bit of
sleight of hand. The object returned from super()
gives you access to the methods on the next level up the
mro, however when you use it to invoke a method,
then the 'self' passed to that method is the second
object, not the instance returned from super().

This is a key point. I wonder whether we might use this thread to draft
a patch to the docs for submission on SourceForge?


Done:

www.python.org/sf/1163367

It's nowhere near perfect, but I think it at least somewhat more
accurately reflects the actual workings of super. If you have any good
clarifications or rewordings, please feel free to add them to the tracker.
While I also agree that the documentation needs a major revamp to
properly show the interworkings of all the new-style class components, a
patch for super is about the best I can offer at the moment. =)

STeVe
Jul 18 '05 #6

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

Similar topics

2
1677
by: Clarence Gardner | last post by:
The super object is considered a solution to the "diamond problem". However, it generally requires that the ultimate base class know that it is last in the method resolution order, and hence it should not itself use super (as well as supplying the ultimate implementation of an overridden method.) However, a problem comes up if that ultimate base class is also the base class for another which inherits from it and another independent base...
11
5035
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 function on pages 89-90. Based on the example on page 90, I wrote this test code : class A(object): def test(self): print 'A'
0
1512
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. It's much nicer for writing cooperative classes. It does have more overhead, but at this stage I'm not so concerned about that - the important thing is it actually works. Note that this uses sys._getframe() magic ...
10
2136
by: Chris Green | last post by:
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...
6
2014
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' .... py> class B(A): .... x = 'b' ....
7
5258
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 inheritance when the base classes are written to expect it, so "Always use super()" seems like bad advice. OTOH sometimes you need super() to get correct behaviour. ISTM "Only use super() when you know you need it" might be
7
1932
by: Pupeno | last post by:
Hello, I have a class called MyConfig, it is based on Python's ConfigParser.ConfigParser. It implements add_section(self, section), which is also implemented on ConfigParser.ConfigParser, which I want to call. So, reducing the problem to the bare minimum, the class (with a useless add_section that shows the problem): .... def add_section(self, section): .... super(MyConfig, self).add_section(section)
4
1721
by: ddtl | last post by:
Hello everybody. Consider the following code: class A(object): def met(self): print 'A.met' class B(A): def met(self):
10
13306
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>"; print(self.text); def __del__(self): self.text = "</BODY></HTML>"; print(self.text);
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.