overriding base class

A

alf

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?
 
C

Carl Banks

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
 
M

Michele Simionato

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
 

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

No members online now.

Forum statistics

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

Latest Threads

Top