In many of the problems I work on, I find that I'll have a custom class, then a container class that stores those objects and methods that acts on all the objects in the class.
For example, consider a curve class, and a container.
class Curve:
def __init__(self, args):
self.label
def getLength(self):
# code that gets length
...
curves = [curve1, curve2, ..., ]
class CurvesContainer:
def __init__(self, curves):
self.curves = curves
...
def setLabels(self, labels):
for curve, label in zip(self.curves, labels):
curve.label = label
def getLengths(self):
lengths = []
for curve in self.curves:
lengths.append(curve.getLength())
return lengths
curve1 = container.curves[1]
However, if we use this setup, we end up with calling container.curves
/self.curves
a lot - kind of ugly!
It seems a way of refactoring this would be to inherit from list
as follows:
class CurvesContainer(list):
def __init__(self, curves):
super().__init__(curves)
def setLabels(self, labels):
for curve, label in zip(self, labels):
curve.label = label
def getLengths(self):
lengths = []
for curve in self:
lengths.append(curve.getLength())
return lengths
curve1 = container[1]
However, I don't see this done much in practice - I'm wondering if this is bad form for some reason?