I have a private method def __pickSide(self): in a parent class that I would like to override in the child class. However, the child class still calls the inherited def __pickSide(self):. How can I override the function? The child class’s function name is exactly the same as the parent’s function name.
I have a private method def __pickSide(self): in a parent class that I would
Share
Let’s look at the easiest example:
The disassembly is as follows:
As you can see, Python mangles function names that begin with two underscores (and accesses to such names!!) to a name that includes the class name – in this case
_A__pickand_B__pick). That means that the class in which a function is defined determines which of the__pickmethods is called.The solution is simple, avoid pseudo-private methods by removing the double underscores. For example, use
_pickinstead of__pick.