MixIn method to call the method it overrides: how?

M

Mac

I have a MixIn class which defines a method foo(), and is then mixed in
with another class by being prepended to that class's __bases__ member,
thus overriding that class's definition of foo(). In my application
though it is necessary for the MixIn's foo() to call the overridden
foo(). How can I do this?

My current hack is to do this:

def foo(): # MixIn's method
orig_bases = self.__class__.__bases__
bases = list(orig_bases)
bases.remove(OptEdgeCache)
self.__class__.__bases__ = tuple(bases)
foo_orig = self.foo
self.__class__.__bases__ = orig_bases

# other stuff here...


Is there a better way? Does the above even work as I think it does?
 
W

wittempj

Something like this will do what you want to achieve. I think the above
does as well what you want, but to me my solution is much more clear

class Base(object):
def foo(self):
print 'Base foo'

class Derived(Base):
def foo(self):
super(Derived, self).foo()
print 'Derived foo'
d = Derived()
d.foo()
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top