Joel Uckelman on Thu, 9 Dec 2004 15:12:13 -0600 (CST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [nimh-dev] virtual member functions


Thus spake "Jon Stewart":
> > Is there any way to make Python treat member functions overriden in a
> > subclass as virtual? I'd like to add a parsesequence() to nimh.Message
> > to override the one from mhlib.Message so I can add the remaining
> > functionality to it, but when I try doing that Python seems not to notice
> > and calls in mhlib.Message to parsesequence() still go to the one in
> > mhlib.Message rather than my version in nimh.Message.
> 
> 
> I thought parsesequence() was defined in folder? Anyway...

Yeah, it is.

s/Message/Folder/g

> All functions can be overridden. From what I understand, Python is just a 
> series of dictionaries acting as symbol tables. Each object gets its own 
> symbol table -- you can actually add methods to an object that are unique 
> to it. If python can't find the method in the object, it looks in the 
> class' symbol table. If unsuccessful, it looks in the base class' symbol 
> tables (depth first).
> 
> So, this should work:
> 
> class Base:
>   def foo(self):
>     print "bar"
> 
> class Derived(Base):
>   def foo(self):
>     print "baz"
> 
> x = Derived()
> x.foo() # print baz
> 
> (This does work in the interpreter.)

That isn't the same as what I'm doing.

What I have is this:

class Base:
  def foo(self):
    print "bar"

  def frobnicate(self):
    self.foo()
 
class Derived(Base):
  def foo(self):
    print "baz"
 
x = Derived()
x.frobnicate() # want to print baz

Here, I'm not directly calling the overridden function; instead, a 
non-overridden function from the base class is. This example code works. 
What doesn't work is doing this with nimh.Folder.parsesequence(). Try 
feeding scan.py a sequence which generates an error message, and the last 
line of the stack trace will be something like this:

  File "/usr/lib/python2.3/mhlib.py", line 365, in parsesequence
    raise Error, "bad message list %s" % seq

If nimh.Folder.parsesequence() were being called, then the one in mhlib.py 
wouldn't be called at all, so something has gone wrong here.

-- 
J.


_______________________________________________
nimh-dev mailing list
nimh-dev@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/nimh-dev