Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > overriding base class

Reply
Thread Tools

overriding base class

 
 
alf
Guest
Posts: n/a
 
      06-29-2007

Hi,


I want to develop a following lib:

lib space user space

A -> B -> | -> user_class


however A and B are abstrac enough to have following:


user_super_base_class -> | -> A -> B -> | -> user_class

user space lib space user spaca



Any idea how to do that?

--
alfz1
 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      06-30-2007
On Jun 29, 7:36 pm, alf <ask@me> wrote:
> Hi,
>
> I want to develop a following lib:
>
> lib space user space
>
> A -> B -> | -> user_class
>
> however A and B are abstrac enough to have following:
>
> user_super_base_class -> | -> A -> B -> | -> user_class
>
> user space lib space user spaca
>
> Any idea how to do that?



One possibility is to use multiple inheritance to get the same
effect. This is easy in Python but wouldn't work in C++. Note: it's
very important use new-style classes, however, so always inherit from
object or some other new-style class.

First, in the library space define A and B. Notice that even though A
derives from object, it calls the "base" class's method.

class A(object):
def method(self):
print "calling A.method"
super(A,self).method()

class B(A):
def method(self):
print "calling B.method"
super(B,self).method()

In user space, declare the base class as so. Note that super is not
called because this is the "real" base.

class SuperBase(object):
def method(self):
print "calling SuperBase.method"

Then, the user class. We use multiple inheritance here, and put
SuperBase at the end. The effect is the same as if A had been derived
from SuperBase.

class User(B,SuperBase):
def method(self):
print "calling User.method"
super(User,self).method()


Calling this User().method() produces the following output:

calling User.method
calling B.method
calling A.method
calling SuperBase.method


Notice that A.method calls SuperBase.method, quite unintuitively for
someone not used to Python's MRO rules. Basically, whenever you have
multiple bases, Python creates a consistent ordering of the bases,
called the Method Resolution Order (MRO), according to precedence.
You can exploit this to "insert" a super base class at the bottom.

You can see what the MRO of a class is with the __mro__ attribute.
For example, User.__mro__ is:

(<class '__main__.User'>,
<class '__main__.B'>,
<class '__main__.A'>,
<class '__main__.SuperBase'>,
<type 'object'>)

Even though A didn't derive directly from SuperBase, it acts as if it
had been, because it's right before SuperBase in the MRO.

Now that I've suggested that, I highly recommend you be sure you're
very acquainted with new-style objects and method resolution order
before attempting this. You need extra care when using MI, even
though this use of it is rather tame.


Cark Banks

 
Reply With Quote
 
 
 
 
Michele Simionato
Guest
Posts: n/a
 
      06-30-2007
On Jun 30, 5:23 am, Carl Banks <pavlovevide...@gmail.com> wrote:

> Now that I've suggested that, I highly recommend you be sure you're
> very acquainted with new-style objects and method resolution order
> before attempting this. You need extra care when using MI, even
> though this use of it is rather tame.
>
> Cark Banks


I would say that the burden on the writer of the hierarchy is not that
much.
The real burder is on the *reader* of the code, which can get easily
confused from where methods are coming. This is way I usually do not
recommend MI: because of the reading and maintenance effort (see for
instance the nightmarish situation in Zope 2). To the OP I would
suggest to
consider containment instead, to consider using proxy objects,
__getattr__ and
other similiar tricks that usually do the job.

Michele Simionato

 
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
Overriding base class methods in the C API Floris Bruynooghe Python 1 01-19-2009 02:05 PM
Problem with method overriding from base class Python 2 03-31-2008 08:37 PM
Overriding base class James Emil Avery C++ 4 07-26-2007 01:39 AM
[Long] Static classes, inheritance and base class funkery - or overriding module subroutines Tim S Perl Misc 3 06-01-2007 06:33 AM
overriding method that returns base class object Stuart McGraw Python 10 02-17-2004 04:48 PM



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