python - Calling Overriden Methods in Derived Class from Base Class -


I was reading about Python Docs and came in this paragraph, which I am not sure about:

Derived classes can override the methods of their base classes. Since there is no special privilege in ways to call other methods of identical objects, a method of base class that calls another method defined in the same base class, can call the method of the derivative class that it Override. (For C ++ programmers: All the methods in Python are effectively virtual.)

Example:

  Class A: def foo (self) : Self.bar) DEF bar (printed): "A to" class B (A): def AFU (self): itself .bar () Def bar (self): Print "From B"  

Does this mean that any object of class A obj = A () can print from "B"? Am I reading this correctly? If this does not make sense, then I am sorry I am in a little confused how the python handles heritage and overriding thanks!

There is no way that superclass can know anything about subclass This is that if you instantiate subclass B, and it processes a method foo () , and overrides one method to bar () , then When you call foo () , which will give the definition of bar () , definition in A is not bar () definition. This superclass writer does not intend - the bar () was expected to call for his definition.


Comments