When I was reading Mark Lutz - "Learning Python Tom 2" book, I faced with the next example of private attributes usage:
class C1:
def meth1(self):
self.__X = 88
def meth2(self):
print(self.__X)
class C2:
def metha(self):
self.__X = 99
def methb(self) :
print(self.__X)
class C3(C1, C2):
pass
instance = C3()
instance.meth1()
instance.metha()
print(instance.__dict__) # {'_C1__X': 88, '_C2__X': 99}
This example perfectly shows how private attributes can solve issues with multiple inheritance. But if we change C1 and C2 classes private attributes to protected attributes, then in the C3 class we will get only one _X variable, defined in the C2 class.
At this point I was really confused, because I still see that lots of attributes/methods in many libraries and frameworks are not private, but protected.
Of course, I've read and met lots of articles and conversations about this topic, but in every single one the usage of the protected attributes is explained as: "It is just an agreement of developers to use protected objects, to protect your class attributes and methods from outside usage".
Yes, I understand well that in python none of the approaches makes class objects really inaccessible from outside usage, because private objects can be accessed from outside as well. But as we saw in the example above, private attributes can at least protect my code from multiple inheritance issues, when protected ones cannot.
So, where and why should I use protected attributes rather then private ?