| Jon Stewart on Thu, 9 Dec 2004 12:05:59 -0600 (CST) |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| Re: [nimh-dev] virtual member functions |
> 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...
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.)
Jon
--
Jon Stewart Advanced Los Angeles C++
stew1@xxxxxxxxxxx http://www.alacpp.org
_______________________________________________
nimh-dev mailing list
nimh-dev@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/nimh-dev