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?
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.A.func_Y"not work"? You don't even callA.func_Yin your code.A.func_XA.func_Y, it's callingself.func_Y.