It seems to me that there's an inherent difficulty in Python when a programmer has to work with a class developed by someone else about which he/she has essential documentation (how to construct it, what functions are available etc) but not details of its implementation. Of course one could examine the source code when things start to go wrong but said class may be entirely robust until someone wants to subclass it.
Consider this trivial example:-
class Base():
def __init__(self):
self._n = 99
@property
def n(self):
return self._n
@n.setter
def n(self, n):
self._n = n
def func(self):
print(self.n)
The key thing here is that Base has a function that relies, in some way, on the attribute _n.
I now decide that I want to develop a class that sub-classes Base. So, I write this:-
class Myclass(Base):
def __init__(self, n):
super().__init__()
self.n = n
Now I'm in trouble because I've just zapped an attribute in my superclass and, potentially, completely broken its functionality.
Are there any development patterns or any other forms of defence against this potential problem?