When creating a subclass in python, how do I ensure that my subclass implementation methods don't unintentionally override an implementation method in a superclass? I'm trying to avoid:
class A:
def a(self):
self._helper_func()
def _helper_func(self):
# do something specific to A
class B(A):
def b(self):
self._helper_func()
def _helper_func(self):
# do something; didn't realize this would mess up a()
I'm thinking the only options are: (1) read the source code of the super class to ensure I'm picking a unique subclass method name (which may break when the superclass is updated); or (2) use __ on all implementation methods in my subclass (usually __ is described as a way for a superclass to prevent subclassing; but I'm thinking it would help a subclass avoid breaking the superclass).
Am I missing something here?
Update @sobolevn basically answered my question. To clarify: all of the discussion I've seen on name-mangling centers around superclasses trying to hide/prevent their methods from being called/overridden (and there's usually comment on how this isn't the python way and users should be respectful of a single leading _). I'm concerned about the very different problem of subclassing a class that I didn't write (and which did not, itself, use name mangling), and choosing names for implementation methods, which could inadvertently break code in the superclass (or its parents) if that name was already used.
Based on what I've read, it seems to me that (a) best practice for writing a subclass of class(es) that I didn't write myself would be to prefix __ to all private methods; but (b) given that all methods are virtual in python and I often will want my subclass to expose some new public/protected method, there is no good alternative to being fully aware of all the non-mangled method names used by all the classes I inherit from, and this awareness would obviate the requirement to worry about (a).