"inherited" keyword in Python?
Alan Gauld
alan.gauld at gssec.bt.co.uk
Wed Dec 6 09:10:45 EST 2000
Carsten Gaebler wrote:
> i.e. instead of saying "superclass.method()" you say "inherited
> method()".
Mostly they are equivalent but they are not identical.
ISTR inherited can only be used on virtual methods.
Static methods of the superclass must be called using
the first style.
Also inheritred can be used for message handling methods
(where the superclass's handler may not have the same name
as the method in the subclass...)
The OP language reference says of 'inherited':
-------------------------
Within a message-handler method, you can call the message-handler
inherited
from the object's ancestor type. Because the name and parameter
of the
method might vary, you can call the inherited method just by
using inherited;
you do not have to include the ancestor method's name.
For example, the WMPaint method described above might be
implemented
as follows:
procedure TMyControl.WMPaint(var Message: TWMPaint);
begin
with Message do begin
...
inherited;
...
end;
end;
The inherited method called will be the one with the same message
index
(in this case, WM_PAINT). For example, if the method declared
above, TMyOtherControl.PaintIt, included an inherited statement,
the method
called would be TMyControl.WMPaint.
Default message handler
It is always safe to call inherited within a message-handler
method.
If the ancestor type does not declare a specific message handler
for
a particular message index, inherited calls the TObject method
DefaultHandler.
---------------------------------
> Can be quite helpful if you change the name of the superclass
It can be useful for that but that's not really its purpose.
You can fake this behaviour in Python by defining a local
'hidden' variable (__inherited, say) and calling that:
class foo(Bar):
__inherited = Bar # the '__' hides it
def spam(self):
__inherited.spam(self)
Alan G.
--
=================================================
This post represents the views of the author
and does not necessarily accurately represent
the views of BT.
More information about the Python-list
mailing list