0

I have:

class A:

    def __init__(self):
        pass

    def func_X(self):
        print("Class A: X")

        self.func_Y()

    def func_Y(self):
        print("Class A: Y")
        print("Why does not work?")


class B(A):

    def __init__(self):
        A.__init__(self)

    def func_Y(self):
        print("Class B: Y")


if __name__ == "__main__":

    TEST = B()
    TEST.func_X()

OUTPUT:

Class A: X
Class B: Y

Question: Why works "B.func_Y", but not "A.func_Y"? This is bug? How to fix it? I need this to work same as in C++. C++ analog:

#include <iostream>

using namespace std;

class A
{
public:
      void func_X() {cout<<"Class A: X"<<endl; func_Y();}
      void func_Y() {cout<<"Class A: Y"<<endl;}
};

class B: public A
{
public:
      void func_Y() {cout<<"Class B: Y"<<endl;}
};


int main(void)
{
    B TEST = B();
    TEST.func_X();

    return 0;
}

OUTPUT:

Class A: X
Class A: Y

I faced this problem for a long time, but have not found a solution. Any idea how to solve this problem?

4
  • 3
    It seems that in Python functions are virtual. If you need it to behave differently, you need to design it differently. C++ and Python are not the same languages, and will behave differently from each other. Deal with it. Commented May 2, 2013 at 7:03
  • What are you asking? In what way does A.func_Y "not work"? You don't even call A.func_Y in your code. Commented May 2, 2013 at 7:03
  • @BrenBarn: It's called in A.func_X Commented May 2, 2013 at 7:06
  • @Blender: That's not calling A.func_Y, it's calling self.func_Y. Commented May 2, 2013 at 7:15

2 Answers 2

4

This happens because you call func_Y() on self. This would probably be the equivalent in C++ of calling func_Y() on this.

Look at this related question.

It seems that you should do a A.func_Y() or a call to super instead of self.func_Y().

Let me know if this helps you.

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

2 Comments

That should be A.func_Y(self).
I don't understand this answer. In Python, when calling from within a member, you have to use self.function(); in C++, this would be this->function();, except that the this-> is implicit.
1

Python resolves everything at run time, using the dynamic type. C++ resolves almost everything at compile time, using the static type. If you want dynamic type resolution, you must tell the compiler this in the static type. In this case, declare func_Y virtual in the base class, and it should work as in Python.

The way name lookup works is actually significantly different in Python and in C++, but in simple cases, the practical results are as if all members in Python were declared virtual in C++. Except that in C++, only functions can be declared virtual; in Python, dynamic lookup affects all members.

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.