Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > MixIn method to call the method it overrides: how?

Reply
Thread Tools

MixIn method to call the method it overrides: how?

 
 
Mac
Guest
Posts: n/a
 
      06-18-2005
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?

 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      06-18-2005
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()

 
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
method mixin? Cnm Cnm Ruby 1 08-20-2010 05:38 AM
Overridding A Method Via A Mixin Andrew Stewart Ruby 15 01-17-2008 11:10 AM
Mixin a module method as a class method Farrel Lifson Ruby 9 10-19-2006 12:06 PM
Class and Mixin with same method name problem petermichaux@yahoo.com Ruby 13 11-07-2005 08:36 AM
calling binding() within a module method (for mixin)????? Ben Ruby 3 11-10-2004 10:24 PM



Advertisments