How to override a member of base class in my derived class so that accesses to such member cause invoking the derived member?
Consider folllowing example? __tmp
is overwritten in B
and should be return in case of a call to tmp()
class A:
__tmp = {"A" : 1,
"B" : 2}
def tmp(self):
return self.__tmp
class B(A):
__tmp = {"A" : 10,
"B" : 20}
def __init__(self):
super().__init__()
b = B()
print(b.tmp()) # Expect to print {'A': 10, 'B': 20} here
Don't use obfuscated variable names:
The problem was that
self.__tmp
is name-mangled behind the scenes by python and resolves toself._A__tmp
sincetmp
is a method of theA
class. You wantedself._B__tmp
. If you had redefined thetmp
method inB
it would have worked, but that's a silly way of doing it, since it defeats one of the purposes of inheritance -- avoiding code duplication.Reserve the use of
__private
variables for when you wish to avoid name clashes with the attributes of subclasses. In this case, you really wish to achive such a naming collition.If you want to make an attribute private, it suffices with a single underscore, since we're all consenting adults here.