3

I am reading python 3.7 documentation. And I am very confused about the following sentences:


"Derived classes may override methods of their base classes. Because methods have no special privileges when calling other methods of the same object, a method of a base class that calls another method defined in the same base class may end up calling a method of a derived class that overrides it. (For C++ programmers: all methods in Python are effectively virtual.)"


Can you show me an example code that "a base class that calls another method defined in the same base class may end up calling a method of a derived class that overrides it."? And here is my understanding:

class A:
def me(self):
    print("This is A")

def idet(self):
    self.me()


class B(A):
    def me(self):
        print("this is B")


a = A()
b = B()
b.me()
b.idet()

the result is

this is B
this is B

I am not sure if it is the case.

And the last question is what does "all methods in Python are effectively virtual" mean? (I am familiar with Java but not C++)

3 Answers 3

2

The example shows this principle exactly. b, which is an instance of B, calls the method ident defined in class A, which in turn calls me. Since B overrides me, B's method is called, and you get this is me printed out.

In C++, methods can't be overridden by default - you have to declare them as virtual to get this behavior. In Python (and in Java, which you mentioned you are familiar with) this is is the default behavior. In Java you can modify a method so it can't be overrided, by defining it as final.

Sign up to request clarification or add additional context in comments.

1 Comment

So these statements are just describing the basic mechanism of overriding like Java. Is there any special features of python's overriding which java do not have?
1

When you derive a class, all the methods of the super class are copied into the base class. When you redefine a method that already exists in the super class while defining methods in the base class (or super class), it is called as overriding. When you override a method from the base class (or super class) in the sub class, it will kinda cut the connections between the method in the base class and the sub class.

In your program, you're calling b.me() first, which is an overrided method; So, it will execute the me() from the class B. Then, you've got b.idet() which is a copied method from the base class A; So, its code will not change. But, when you look closely in the body of the idet() method, what it does is that it will call the me() method of the class which it is being called from. In this case, as the class calling that method is the class B, it will execute the me() method from the class B.

idet() has got self.me(); self keyword references to the attributes/methods class within which it is written.

Comments

0

I think the Python3 documentation could have also mentioned that when an instance of class B, 'b', refers to a method that is pulled from class A, it passes itself into that method as the first argument (self). Therefore any reference to "self" inside any method called by 'b' (including its inherited methods) will first search class B functions before class A functions, even if the method called by 'b' was derived from class A.

b.idet() is equivalent to A.idet(b) which calls b.me()

"a method of a base class that calls another method defined in the same base class may end up calling a method of a derived class that overrides it"

I can see how this seems misleading, and I think its because the method idet(self) of base class A only calls another method defined in the same base class (A) if "self" refers to an instance of A, but in the above scenario when "self" refers to an instance of subclass B, idet(self) doesn't really call another method defined in A, it calls a method defined in B that overrides A.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.