style question: anything wrong with super(type(self), self).f() ?

A

Adam Monsen

Is there anything wrong with using something like super(type(self),
self).f() to avoid having to hardcode a type? For example:

class A(object):
def f(self):
print "in A.f()"

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

obj = A()
obj.f() # prints "in A.f()"


By "wrong" I mean, is there any reason why this is just a Bad Idea?
Seems helpful to me, if I change the name of the 'B' class, I don't
have to change super() calls as well.
 
T

Tom Anderson

Is there anything wrong with using something like super(type(self),
self).f() to avoid having to hardcode a type?

What happens when that method gets called by an overriding method in a
derived class?
For example:

class A(object):
def f(self):
print "in A.f()"

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

obj = A()
obj.f() # prints "in A.f()"

Continuing your example:

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

obj = C()
obj.f()

Think about what's going to happen. Then try it!
By "wrong" I mean, is there any reason why this is just a Bad Idea?

That rather depends if the behaviour i demonstrate above is useful to you.

:)
Seems helpful to me, if I change the name of the 'B' class, I don't have
to change super() calls as well.

It would indeed be very useful.

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top