Suppose that I have the following python base class:
class BaseClass(object):
def a():
"""This method uses method b(), defined in the inheriting class"""
And also a class that inherites BaseClass:
class UsedByUser(BaseClass):
def b():
"""b() is defined here, yet is used by the base class"""
My user would only create instances of class UsedByUser. Typical use would be:
if __name__ == '__main__':
# initialize the class used by the user
usedByUser = UsedByUser()
# invoke method a()
usedByUser.a()
My questions is, is the above use problematic? is this a valid approach, or must I also define method b() in BaseClass and then override it in UsedByUser?
I would define the
bmethod in theBaseClasstoo:Remember: explicit is better than implicit and given that the method
aneeds the methodbanyways, better raise a meaningful exception rather than a generalAttributeError.It is worth to point out that this is absolutely NOT needed from a syntactic point of view, but it adds clarity to the code and enforces the subclass to provide an implementation.