Kernel: Python 3 (system-wide)
How/Why private attribtues in python classes?
Scenario 1
We have a parent/child inheritance, which uses the same variable names.
In [1]:
In [2]:
In [3]:
123
Note, the get_x
method comes from the parent class – while the value x
from the child class.
In [4]:
246
Here is the catch: there is only one x
in play and therefore the child class overwrote the parent classes' x
attribute.
In [5]:
246
In [0]:
In [0]:
Scenario 2
We use Python's name-mangling (two leading underscores are replaced by _[class name]_
) to introduce two x
attributes in total. Therefore, we avoid them being overwritten.
In [6]:
In [7]:
In [8]:
123
In [9]:
246
In [10]:
123
In [0]: