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

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 2343
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 "<interactive 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__(cls, '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.com> wrote in message
news:1110396167.53bd67fed97c9e826a92b284283b1455@t eranews...
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.com> wrote in message
news:1110396167.53bd67fed97c9e826a92b284283b1455@t eranews...
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.com> wrote in message
news:1110396167.53bd67fed97c9e826a92b284283b1455@t eranews...
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
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...
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....
10
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...
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'...
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...
7
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...
4
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
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>";...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.