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

super() and type()

I see super documented, and in use, as below (from the Python documentation)

class C(B):
def meth(self, arg):
super(C, self).meth(arg)

I'd like to not write C all the time, so is there any problem with writing:

class C(B):
def meth(self, arg):
super(type(self), self).meth(arg)
This seems to work in practice but I don't see it used anywhere and
I'm worried what I might be missing.

This was especially brought to my attention because pylint flags the
second usage as invalid, and I'm not sure if it should be considered a
false positive or not.
Nov 27 '06 #1
2 1917
Chris Mellon wrote:
I see super documented, and in use, as below (from the Python documentation)

class C(B):
def meth(self, arg):
super(C, self).meth(arg)

I'd like to not write C all the time, so is there any problem with writing:

class C(B):
def meth(self, arg):
super(type(self), self).meth(arg)
This seems to work in practice but I don't see it used anywhere and
I'm worried what I might be missing.

This was especially brought to my attention because pylint flags the
second usage as invalid, and I'm not sure if it should be considered a
false positive or not.
PyLint is right. Try running this:
class A(object):
def meth(self,arg):
print "hello"

class B(A):
def meth(self,arg):
super(type(self), self).meth(arg)

class C(B):
def meth(self,arg):
super(type(self), self).meth(arg)

print C().meth(1)
The reason this type(self) returns the type of the object (which
doesn't change), NOT the type of the class it was defined in. That is,
type(self) returns C even in the function B.meth. Since C's superclass
is B, B.meth ends up calling B.meth again, and you get infinite
recursion.

Unfortunately, short of hackery, you're stuck with having to write out
super(C,self).
Carl Banks

Nov 27 '06 #2
Chris Mellon wrote in
news:ma**************************************@pyth on.org in
comp.lang.python:
I see super documented, and in use, as below (from the Python
documentation)

class C(B):
def meth(self, arg):
super(C, self).meth(arg)

I'd like to not write C all the time, so is there any problem with
writing:

class C(B):
def meth(self, arg):
super(type(self), self).meth(arg)
This seems to work in practice but I don't see it used anywhere and
I'm worried what I might be missing.
Have you considered what happens if somebody writes:

class D(C):
def meth(self, arg):
super( D, self ).meth( arg )
>
This was especially brought to my attention because pylint flags the
second usage as invalid, and I'm not sure if it should be considered a
false positive or not.
Nope, the type argument to super tells it where you are in the
type hierarchy, type(self) is always the top of the hierarchy.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Nov 27 '06 #3

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...
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...
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...
5
by: Erwan Adam | last post by:
Hi all, I have a problem with the use of descriptors and super. The file is : # --------------- class Desc(object): def __init__(self, class_name): self.class_name = class_name
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
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,...

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.