The following piece of Python code uses a superclass solely as the repository of functions that one of more subclasses may draw from:
class Class(object):
''' A trivial repository for functions to be inherited '''
def function(self):
print self
class Subclass(Class):
''' A subclass where the action is'''
def __init__(self):
pass
subitem = Subclass()
subitem.function()
This seems to be the minimum amount of coding that fits that purpose.
However, I received a non-argued hint that a better way to code the same would be having a constructor in the superclass and calling it inside the subclass constructor (seemingly a sort of tighter coupling):
class Class(object):
''' The function repository, now featuring a constructor'''
def __init__(self):
pass
def function(self):
print self
class Subclass(Class):
''' The same subclass, but its constructor invokes the superclass constructor'''
def __init__(self):
Class.__init__(self)
subitem = Subclass()
subitem.function()
The question is... In which way would the second way of subclassing be 'superior' to the first one? Superior might mean more general, applicable to a greater variety of situations and so forth.
Thanks for thinking along.