Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Skipping a superclass

Reply
Thread Tools

Skipping a superclass

 
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-02-2009
I have a series of subclasses like this:

class A(object):
def method(self, *args):
print "Lots of work gets done here in the base class"

class B(A):
def method(self, *args):
print "A little bit of work gets done in B"
super(B, self).method(*args)

class C(B):
def method(self, *args):
print "A little bit of work gets done in C"
super(C, self).method(*args)


However, the work done in C.method() makes the work done in B.method()
obsolete: I want one to run, or the other, but not both. C does need to
inherit from B, for the sake of the other methods, so I want C.method()
*only* to skip B while still inheriting from A. (All other methods have
to inherit from B as normal.)

So what I have done is change the call to super in C to super(B, self)
instead of super(C, self). It seems to work, but is this safe to do? Or
are there strange side-effects I haven't seen yet?



--
Steven
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      08-02-2009
On Sun, Aug 2, 2009 at 5:36 AM, Steven
D'Aprano<> wrote:
> I have a series of subclasses like this:
>
> class A(object):
> Â* Â*def method(self, *args):
> Â* Â* Â* Â*print "Lots of work gets done here in the base class"
>
> class B(A):
> Â* Â*def method(self, *args):
> Â* Â* Â* Â*print "A little bit of work gets done in B"
> Â* Â* Â* Â*super(B, self).method(*args)
>
> class C(B):
> Â* Â*def method(self, *args):
> Â* Â* Â* Â*print "A little bit of work gets done in C"
> Â* Â* Â* Â*super(C, self).method(*args)
>
>
> However, the work done in C.method() makes the work done in B.method()
> obsolete: I want one to run, or the other, but not both. C does need to
> inherit from B, for the sake of the other methods, so I want C.method()
> *only* to skip B while still inheriting from A. (All other methods have
> to inherit from B as normal.)
>
> So what I have done is change the call to super in C to super(B, self)
> instead of super(C, self). It seems to work, but is this safe to do? Or
> are there strange side-effects I haven't seen yet?


Barring some true weirdness in super(), I don't /think/ so. It
obviously works fine in the single-inheritance case, and (after some
brute-force testing) the only permitted multiple inheritances
involving C and (A or B) always have C ahead of B in the MRO (method
resolution order).

The fact that you're wanting to bypass a superclass method might
suggest however, that your hierarchy could be structured better. Have
you considered moving B.method() into another, new class (e.g. newB)
which subclasses B? Then C can inherit from B without inheriting the
unwanted method, while B's functionality still exists, just under a
new name (newB).

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
 
 
 
Miles Kaufmann
Guest
Posts: n/a
 
      08-03-2009
On Aug 2, 2009, at 5:36 AM, Steven D'Aprano wrote:
> I have a series of subclasses like this:
>
> class A(object):
> def method(self, *args):
> print "Lots of work gets done here in the base class"
>
> class B(A):
> def method(self, *args):
> print "A little bit of work gets done in B"
> super(B, self).method(*args)
>
> class C(B):
> def method(self, *args):
> print "A little bit of work gets done in C"
> super(C, self).method(*args)
>
>
> However, the work done in C.method() makes the work done in B.method()
> obsolete: I want one to run, or the other, but not both. C does need
> to
> inherit from B, for the sake of the other methods, so I want
> C.method()
> *only* to skip B while still inheriting from A. (All other methods
> have
> to inherit from B as normal.)


This might not be applicable to the larger problem you're trying to
solve, but for this sample, I would write it as:

class A(object):
def method(self, *args):
self._method(*args)
print "Lots of work gets done here in the base class"
def _method(self, *args):
pass # or perhaps raise NotImplemented

class B(A):
def _method(self, *args):
print "A little bit of work gets done in B"

class C(B):
def _method(self, *args):
print "A little bit of work gets done in C"

> So what I have done is change the call to super in C to super(B, self)
> instead of super(C, self). It seems to work, but is this safe to do?
> Or
> are there strange side-effects I haven't seen yet?


In a diamond-inheritance situation, you may end up skipping methods
besides just B.method().

-Miles

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check if another object is my superclass from a function in aneven higher superclass? bart van deenen C++ 6 03-03-2009 02:44 PM
calling superclass __init__ when superclass is object Evan Klitzke Python 0 08-02-2007 05:15 AM
sqlDataReader skipping first row in loop branton ellerbee ASP .Net 3 06-02-2004 09:17 AM
Forms Authentication and skipping a page Tommy Martin ASP .Net 0 12-15-2003 08:45 PM
Newbie question - trying to get a handle on until text / line skipping Chris Vidal Perl 3 07-18-2003 11:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57