I am trying to understand how class inheritance works in Python 3, in particular how private fields interact with local and inherited methods. Here are some examples to illustrate the issue.
First, if a variable var
in the Superclass is public, then any method in the Subclass will also be able to alter it:
class Superclass:
var = 1
def getVar(self):
print(self.var)
class Subclass(Superclass):
def __init__(self):
self.var = 123
my_object = Subclass()
my_object.getVar() # outputs 123
The same is not true if the variable __var
in the Superclass is private, any inherited method will ignore modifications done by the Subclass:
class Superclass:
__var = 1
def getVar(self):
print(self.__var)
class Subclass(Superclass):
def __init__(self):
self.__var = 123
my_object = Subclass()
my_object.getVar() # outputs 1!
Local methods in the Subclass can alter it:
class Superclass:
__var = 1
class Subclass(Superclass):
def __init__(self):
self.__var = 123
def getVar(self):
print(self.__var)
my_object = Subclass()
my_object.getVar() # outputs 123
But in order to use an inherited method with the altered value, I must use self._Superclass__var
instead of self.__var
in the Subclass:
class Superclass:
__var = 1
def getVar(self):
print(self.__var)
class Subclass(Superclass):
def __init__(self):
self._Superclass__var = 123
my_object = Subclass()
my_object.getVar() # outputs 123
Why is this the case? Are private fields not being inherited by the subclasses and therefore the variable self.__var
inside the Subclass
is NOT pointing to the same value the variable self.__var
inside Superclass
?
__
you invoke name mangling, where the name of the "owning" class is automatically inserted so as to help prevent attribute names from conflicting in subclasses. It doesn't prevent you from accessing it, it just requires that you include the owning class's name. See docs.python.org/3/tutorial/classes.html#private-variables – kindall