One of the commonly asked questions about wxPython is something like, "I tried to override method FooBar but it is never called, why not?"

Python lets you override any method in Python classes and the interpreter will dynamically bind to it at runtime so many users assume that you can do this with wxPython classes too. Unfortunately it's not that simple. Keep in mind that inside most wxPython objects is a C++ object, and in most cases that C++ object doesn't know that the Python object even exists.

In Python, when one method calls another you know that if the method is overridden in a derived class, and the object is an instance of that class, that the method in the derived class will be the one that is called. However with wxPython, if a C++ method is called from C++, and it's been overridden in Python then only the C++ method is called because the overriding mechanism is different in the different languages so the call will not be automatically propagated to the Python class.

Of course with every rule there are exceptions and the same is true here. There are some methods in wx classes that are meant to be overriden by design, and if they aren't some part of the functionality is missing. In these cases wxPython derives a special C++ class that is Python aware and is able to reflect the C++ method call to the Python method, if it exists. There is a fair amount of runtime overhead, and a large amount of code bloat to make this happen so it is not done in all cases, only where it makes sense or is necessary.

OverridingMethods (last edited 2008-08-04 17:09:58 by 69-30-77-125)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.