overriding base class
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?
Re: overriding base class
On Jun 29, 7:36 pm, alf <ask@me> wrote:
> 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.
print "calling A.method"
print "calling B.method"
In user space, declare the base class as so. Note that super is not
called because this is the "real" base.
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
print "calling User.method"
Calling this User().method() produces the following output:
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:
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.
Re: overriding base class
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
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
consider containment instead, to consider using proxy objects,
other similiar tricks that usually do the job.
|All times are GMT. The time now is 10:16 AM.|
Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.